summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT.Tablet/Forms.cs
blob: 2f6390995984951c26b94aa86bedc048b45e5fe2 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
´╗┐using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Windows.ApplicationModel.Activation;
using Windows.Foundation.Metadata;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#if WINDOWS_UWP
using Xamarin.Forms.Platform.UWP;

#else
using Xamarin.Forms.Platform.WinRT;

#endif

namespace Xamarin.Forms
{
	public static class Forms
	{
		const string LogFormat = "[{0}] {1}";

		static ApplicationExecutionState s_state;
		static bool s_isInitialized;
#if WINDOWS_UWP
		public static void Init(IActivatedEventArgs launchActivatedEventArgs, IEnumerable<Assembly> rendererAssemblies = null)
#else
		public static void Init(IActivatedEventArgs launchActivatedEventArgs)
#endif
		{
			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(GetTabletResources());

			Device.Idiom = TargetIdiom.Tablet;
			Device.PlatformServices = new WindowsPlatformServices(Window.Current.Dispatcher);
			Device.Info = new WindowsDeviceInfo();

#if WINDOWS_UWP
			switch (DetectPlatform())
			{
				case Windows.Foundation.Metadata.Platform.Windows:
					Device.Idiom = TargetIdiom.Desktop;
					break;
				case Windows.Foundation.Metadata.Platform.WindowsPhone:
					Device.Idiom = TargetIdiom.Phone;
					break;
				default:
					Device.Idiom = TargetIdiom.Tablet;
					break;
			}
#endif
			ExpressionSearch.Default = new WindowsExpressionSearch();

#if WINDOWS_UWP
			Registrar.ExtraAssemblies = rendererAssemblies?.ToArray();
#endif

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

			s_isInitialized = true;
			s_state = launchActivatedEventArgs.PreviousExecutionState;

#if WINDOWS_UWP
			SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
#endif
		}

		static Windows.Foundation.Metadata.Platform DetectPlatform()
		{
#if WINDOWS_UWP
			bool isHardwareButtonsAPIPresent = ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");

			if (isHardwareButtonsAPIPresent)
				return Windows.Foundation.Metadata.Platform.WindowsPhone;
#endif
			return Windows.Foundation.Metadata.Platform.Windows;
		}

		static Windows.UI.Xaml.ResourceDictionary GetTabletResources()
		{
			return new Windows.UI.Xaml.ResourceDictionary {
#if !WINDOWS_UWP
				Source = new Uri("ms-appx:///Xamarin.Forms.Platform.WinRT.Tablet/TabletResources.xbf")
#else
				Source = new Uri("ms-appx:///Xamarin.Forms.Platform.UAP/Resources.xbf")
#endif
			};
		}

#if WINDOWS_UWP
		static void OnBackRequested(object sender, BackRequestedEventArgs e)
		{
			Application app = Application.Current;
			if (app == null)
				return;

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

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

			e.Handled = platform.BackButtonPressed();
		}
#endif
	}
}