summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/FormsApplicationDelegate.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/FormsApplicationDelegate.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/FormsApplicationDelegate.cs85
1 files changed, 85 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/FormsApplicationDelegate.cs b/Xamarin.Forms.Platform.MacOS/FormsApplicationDelegate.cs
new file mode 100644
index 0000000..4899698
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/FormsApplicationDelegate.cs
@@ -0,0 +1,85 @@
+´╗┐using System;
+using System.ComponentModel;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public abstract class FormsApplicationDelegate : NSApplicationDelegate
+ {
+ Application _application;
+ bool _isSuspended;
+
+ public abstract NSWindow MainWindow { get; }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && _application != null)
+ _application.PropertyChanged -= ApplicationOnPropertyChanged;
+
+ base.Dispose(disposing);
+ }
+
+ protected void LoadApplication(Application application)
+ {
+ if (application == null)
+ throw new ArgumentNullException(nameof(application));
+
+ Application.Current = application;
+ _application = application;
+
+ application.PropertyChanged += ApplicationOnPropertyChanged;
+ }
+
+ public override void DidFinishLaunching(Foundation.NSNotification notification)
+ {
+ if (MainWindow == null)
+ throw new InvalidOperationException("Please provide a main window in your app");
+
+ MainWindow.Display();
+ MainWindow.MakeKeyAndOrderFront(NSApplication.SharedApplication);
+ if (_application == null)
+ throw new InvalidOperationException("You MUST invoke LoadApplication () before calling base.FinishedLaunching ()");
+
+ SetMainPage();
+ _application.SendStart();
+ }
+
+ public override void DidBecomeActive(Foundation.NSNotification notification)
+ {
+ // applicationDidBecomeActive
+ // execute any OpenGL ES drawing calls
+ if (_application == null || !_isSuspended) return;
+ _isSuspended = false;
+ _application.SendResume();
+ }
+
+ public override async void DidResignActive(Foundation.NSNotification notification)
+ {
+ // applicationWillResignActive
+ if (_application == null) return;
+ _isSuspended = true;
+ await _application.SendSleepAsync();
+ }
+
+ void ApplicationOnPropertyChanged(object sender, PropertyChangedEventArgs e)
+ {
+ if (e.PropertyName == nameof(Application.MainPage))
+ UpdateMainPage();
+ }
+
+ void SetMainPage()
+ {
+ UpdateMainPage();
+ }
+
+ void UpdateMainPage()
+ {
+ if (_application.MainPage == null)
+ return;
+
+ var platformRenderer = (PlatformRenderer)MainWindow.ContentViewController;
+ MainWindow.ContentViewController = _application.MainPage.CreateViewController();
+ (platformRenderer?.Platform as IDisposable)?.Dispose();
+ }
+ }
+} \ No newline at end of file