2 using System.ComponentModel;
3 using System.Diagnostics;
4 using System.Threading.Tasks;
5 using System.Reflection;
7 using Tizen.Applications;
8 using Xamarin.Forms.Internals;
9 using ELayout = ElmSharp.Layout;
10 using DeviceOrientation = Xamarin.Forms.Internals.DeviceOrientation;
11 using Xamarin.Forms.Platform.Tizen.Native;
13 namespace Xamarin.Forms.Platform.Tizen
16 public class FormsApplication : CoreUIApplication
18 ITizenPlatform _platform;
19 Application _application;
23 protected FormsApplication()
25 _isInitialStart = true;
29 /// Gets the main window or <c>null</c> if it's not set.
31 /// <value>The main window or <c>null</c>.</value>
32 public Window MainWindow
45 public ELayout BaseLayout
50 protected override void OnPreCreate()
53 Application.ClearCurrent();
54 PreloadedWindow window = PreloadedWindow.GetInstance() ?? new PreloadedWindow();
55 BaseLayout = window.BaseLayout;
56 MainWindow = window.Window;
59 protected override void OnTerminate()
62 if (_platform != null)
68 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
70 base.OnAppControlReceived(e);
72 if (!_isInitialStart && _application != null)
74 _application.SendResume();
76 _isInitialStart = false;
79 protected override void OnPause()
82 if (_application != null)
84 _application.SendSleep();
88 protected override void OnResume()
91 if (_application != null)
93 _application.SendResume();
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 public static Func<Task> RequestingUserConsentFunc { get; set; } = null;
100 public async void LoadApplication(Application application)
102 if (RequestingUserConsentFunc != null)
104 await RequestingUserConsentFunc();
107 if (null == MainWindow)
109 throw new InvalidOperationException("MainWindow is not prepared. This method should be called in OnCreated().");
112 if (null == application)
114 throw new ArgumentNullException(nameof(application));
116 _application = application;
117 Application.Current = application;
118 application.SendStart();
119 application.PropertyChanged += new PropertyChangedEventHandler(this.AppOnPropertyChanged);
120 SetPage(_application.MainPage);
123 void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args)
125 if ("MainPage" == args.PropertyName)
127 SetPage(_application.MainPage);
131 void SetPage(Page page)
133 if (!Forms.IsInitialized)
135 throw new InvalidOperationException("Call Forms.Init (UIApplication) before this");
138 #pragma warning disable CS0618 // Type or member is obsolete
139 // The Platform property is no longer necessary, but we have to set it because some third-party
140 // library might still be retrieving it and using it
141 if (_application != null)
143 _application.Platform = _platform;
145 #pragma warning restore CS0618 // Type or member is obsolete
147 _platform.HasAlpha = MainWindow.Alpha;
148 _platform.SetPage(page);
150 Device.BeginInvokeOnMainThread(() => Native.NativeFactory.DeleteUnusedNative());
153 void InitializeWindow()
155 Debug.Assert(MainWindow != null, "Window cannot be null");
160 if (BaseLayout == null)
162 var conformant = new Conformant(MainWindow);
165 var layout = new ELayout(conformant);
166 layout.SetTheme("layout", "application", "default");
170 conformant.SetContent(BaseLayout);
173 MainWindow.AvailableRotations = DisplayRotation.Degree_0 | DisplayRotation.Degree_90 | DisplayRotation.Degree_180 | DisplayRotation.Degree_270;
175 MainWindow.Deleted += (s, e) =>
180 Device.Info.CurrentOrientation = MainWindow.GetDeviceOrientation();
182 MainWindow.RotationChanged += (sender, e) =>
184 Device.Info.CurrentOrientation = MainWindow.GetDeviceOrientation();
187 MainWindow.BackButtonPressed += (sender, e) =>
189 if (_platform != null)
191 if (!_platform.SendBackButtonPressed())
198 _platform = Platform.CreatePlatform(BaseLayout);
199 BaseLayout.SetContent(_platform.GetRootNativeView());
200 _platform.RootNativeViewChanged += (s, e) => BaseLayout.SetContent(e.RootNativeView);
205 Run(System.Environment.GetCommandLineArgs());
209 /// Exits the application's main loop, which initiates the process of its termination
211 public override void Exit()
213 if (_platform == null)
215 Log.Warn("Exit was already called or FormsApplication is not initialized yet.");
225 Log.Error("Exception thrown from Dispose: {0}", e.Message);
231 static class WindowExtension
233 public static DeviceOrientation GetDeviceOrientation(this Window window)
235 DeviceOrientation orientation = DeviceOrientation.Other;
236 var isPortraitDevice = Forms.NaturalOrientation.IsPortrait();
237 switch (window.Rotation)
241 orientation = isPortraitDevice ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
246 orientation = isPortraitDevice ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;