summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/FormsApplicationPage.cs
blob: 3fb3ddc0cdeb9979013eb28cd71ae190f97694d9 (plain)
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
using System.ComponentModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class FormsApplicationPage : PhoneApplicationPage
	{
		Application _application;
		Platform _platform;

		protected FormsApplicationPage()
		{
			PhoneApplicationService.Current.Launching += OnLaunching;
			PhoneApplicationService.Current.Activated += OnActivated;
			PhoneApplicationService.Current.Deactivated += OnDeactivated;
			PhoneApplicationService.Current.Closing += OnClosing;

			MessagingCenter.Send(this, Forms.WP8DeviceInfo.BWPorientationChangedName, Orientation.ToDeviceOrientation());
			OrientationChanged += OnOrientationChanged;
			//DeserializePropertyStore ();
		}

		protected void LoadApplication(Application application)
		{
			Application.Current = application;
			application.PropertyChanged += ApplicationOnPropertyChanged;
			_application = application;

			// Hack around the fact that OnLaunching will haev already happened by this point, sad but needed.
			application.SendStart();

			SetMainPage();
		}

		void ApplicationOnPropertyChanged(object sender, PropertyChangedEventArgs args)
		{
			if (args.PropertyName == "MainPage")
				SetMainPage();
		}

		void OnActivated(object sender, ActivatedEventArgs e)
		{
			// TODO : figure out consistency of get this to fire
			// Check whether tombstoned (terminated, but OS retains information about navigation state and state dictionarys) or dormant
			_application.SendResume();
		}

		// when app gets tombstoned, user press back past first page
		async void OnClosing(object sender, ClosingEventArgs e)
		{
			// populate isolated storage.
			//SerializePropertyStore ();
			await _application.SendSleepAsync();
		}

		async void OnDeactivated(object sender, DeactivatedEventArgs e)
		{
			// populate state dictionaries, properties
			//SerializePropertyStore ();
			await _application.SendSleepAsync();
		}

		void OnLaunching(object sender, LaunchingEventArgs e)
		{
			// TODO : not currently firing, is fired before MainPage ctor is called
			_application.SendStart();
		}

		void OnOrientationChanged(object sender, OrientationChangedEventArgs e)
		{
			MessagingCenter.Send(this, Forms.WP8DeviceInfo.BWPorientationChangedName, e.Orientation.ToDeviceOrientation());
		}

		void SetMainPage()
		{
			if (_platform == null)
				_platform = new Platform(this);

			_platform.SetPage(_application.MainPage);

			if (!ReferenceEquals(Content, _platform))
				Content = _platform.GetCanvas();
		}
	}
}