-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
MainActivity.cs
143 lines (119 loc) · 3.65 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using Android.App;
using Android.OS;
#if __ANDROID_29__
using AndroidX.Fragment.App;
using Fragment = AndroidX.Fragment.App.Fragment;
using FragmentTransaction = AndroidX.Fragment.App.FragmentTransaction;
#else
using Android.Support.V4.App;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentTransaction = Android.Support.V4.App.FragmentTransaction;
#endif
using Android.Views;
using Embedding.XF;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using View = Android.Views.View;
using Button = Android.Widget.Button;
namespace Embedding.Droid
{
[Activity(Label = "Embedding.Droid", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : FragmentActivity
{
Fragment _hello;
Fragment _alertsAndActionSheets;
Fragment _webview;
Fragment _openUri;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, null);
SetContentView (Resource.Layout.Main);
var ft = SupportFragmentManager.BeginTransaction();
ft.Replace(Resource.Id.fragment_frame_layout, new MainFragment(), "main");
ft.Commit();
}
public void ShowHello()
{
if (_hello == null)
{
_hello = new Hello().CreateSupportFragment(this);
}
ShowEmbeddedPageFragment(_hello);
}
public void ShowWebView()
{
if (_webview == null)
{
_webview= new WebViewExample().CreateSupportFragment(this);
}
ShowEmbeddedPageFragment(_webview);
}
public void ShowOpenUri()
{
if (_openUri == null)
{
_openUri = new OpenUri().CreateSupportFragment(this);
}
ShowEmbeddedPageFragment(_openUri );
}
public void ShowAlertsAndActionSheets()
{
if (_alertsAndActionSheets== null)
{
_alertsAndActionSheets = new AlertsAndActionSheets().CreateSupportFragment(this);
}
ShowEmbeddedPageFragment(_alertsAndActionSheets);
}
void ShowEmbeddedPageFragment(Fragment fragment)
{
FragmentTransaction ft = SupportFragmentManager.BeginTransaction();
ft.AddToBackStack(null);
ft.Replace(Resource.Id.fragment_frame_layout, fragment, "hello");
ft.Commit();
}
public void LaunchSecondActivity()
{
StartActivity(typeof(SecondActivity));
}
}
public class MainFragment : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.MainFragment, container, false);
var showEmbeddedButton = view.FindViewById<Button>(Resource.Id.showEmbeddedButton);
var showAlertsActionSheets = view.FindViewById<Button>(Resource.Id.showAlertsActionSheets);
var showWebView = view.FindViewById<Button>(Resource.Id.showWebView);
var showOpenUri = view.FindViewById<Button>(Resource.Id.showOpenUri);
var launchSecondActivity = view.FindViewById<Button>(Resource.Id.launchSecondActivity);
showEmbeddedButton.Click += ShowEmbeddedClick;
showAlertsActionSheets.Click += ShowAlertsActionSheetsClick;
showWebView.Click += ShowWebViewOnClick;
showOpenUri.Click += ShowOpenUriClick;
launchSecondActivity.Click += LaunchSecondActivityOnClick;
return view;
}
void ShowWebViewOnClick(object sender, EventArgs eventArgs)
{
((MainActivity)Activity).ShowWebView();
}
void LaunchSecondActivityOnClick(object sender, EventArgs e)
{
((MainActivity)Activity).LaunchSecondActivity();
}
void ShowAlertsActionSheetsClick(object sender, EventArgs eventArgs)
{
((MainActivity)Activity).ShowAlertsAndActionSheets();
}
void ShowEmbeddedClick(object sender, EventArgs e)
{
((MainActivity)Activity).ShowHello();
}
void ShowOpenUriClick(object sender, EventArgs e)
{
((MainActivity)Activity).ShowOpenUri();
}
}
}