summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT.Phone/Forms.cs
blob: 62b4624c5e8407f980865d102b4d038496b55b01 (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
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Diagnostics;
using Windows.ApplicationModel.Activation;
using Windows.Phone.UI.Input;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.WinRT;

namespace Xamarin.Forms
{
	public static class Forms
	{
		public static void Init (IActivatedEventArgs launchActivatedEventArgs)
		{
			if (s_isInitialized)
				return;
			
			var accent = (SolidColorBrush)Windows.UI.Xaml.Application.Current.Resources["SystemColorControlAccentBrush"];
			Color.Accent = Color.FromRgba (accent.Color.R, accent.Color.G, accent.Color.B, accent.Color.A);

			Log.Listeners.Add (new DelegateLogListener ((c, m) => Debug.WriteLine (LogFormat, c, m)));

			Windows.UI.Xaml.Application.Current.Resources.MergedDictionaries.Add (GetPhoneResources());

			Device.OS = TargetPlatform.Windows;
			Device.PlatformServices = new WindowsPhonePlatformServices (Window.Current.Dispatcher);
			Device.Info = new WindowsDeviceInfo();
			Device.Idiom = TargetIdiom.Phone;
			
			Ticker.Default = new WindowsTicker();

			ExpressionSearch.Default = new WindowsExpressionSearch();

			Registrar.RegisterAll (new[] {
				typeof (ExportRendererAttribute),
				typeof (ExportCellAttribute),
				typeof (ExportImageSourceHandlerAttribute)
			});

			MessagingCenter.Subscribe<Page, bool> (Device.PlatformServices, Page.BusySetSignalName, OnPageBusy);

			HardwareButtons.BackPressed += OnBackPressed;

			s_isInitialized = true;
			s_state = launchActivatedEventArgs.PreviousExecutionState;
		}

		static void OnBackPressed (object sender, BackPressedEventArgs e)
		{
			Application app = Application.Current;
			if (app == null)
				return;

			Page page = app.MainPage;
			if (page == null)
				return;

			var platform = page.Platform as Platform.WinRT.Platform;
			if (platform == null)
				return;

			e.Handled = platform.BackButtonPressed ();
		}

		static ApplicationExecutionState s_state;
		static bool s_isInitialized;

		const string LogFormat = "[{0}] {1}";

		static async void OnPageBusy (Page sender, bool enabled)
		{
			StatusBar status = StatusBar.GetForCurrentView ();
			if (enabled) {
				status.ProgressIndicator.ProgressValue = null;
				await status.ProgressIndicator.ShowAsync ();
			} else
				await status.ProgressIndicator.HideAsync ();
		}

		static Windows.UI.Xaml.ResourceDictionary GetPhoneResources ()
		{
			return new Windows.UI.Xaml.ResourceDictionary {
				Source = new Uri ("ms-appx:///Xamarin.Forms.Platform.WinRT.Phone/PhoneResources.xbf")
			};
		}

		static Windows.UI.Xaml.ResourceDictionary GetResources (UserControl control)
		{
			var gresources = control.Resources.MergedDictionaries[0];
			control.Resources.MergedDictionaries.Remove (gresources);

			return gresources;
		}
	}
}