forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForms.cs
112 lines (90 loc) · 3.36 KB
/
Forms.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation.Metadata;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.UWP;
namespace Xamarin.Forms
{
public static partial class Forms
{
const string LogFormat = "[{0}] {1}";
static ApplicationExecutionState s_state;
public static bool IsInitialized { get; private set; }
public static void Init(IActivatedEventArgs launchActivatedEventArgs, IEnumerable<Assembly> rendererAssemblies = null)
{
if (IsInitialized)
return;
var accent = (SolidColorBrush)Windows.UI.Xaml.Application.Current.Resources["SystemColorControlAccentBrush"];
Color.SetAccent(accent.ToFormsColor());
Log.Listeners.Add(new DelegateLogListener((c, m) => Debug.WriteLine(LogFormat, c, m)));
Windows.UI.Xaml.Application.Current.Resources.MergedDictionaries.Add(GetTabletResources());
Device.SetIdiom(TargetIdiom.Tablet);
Device.SetFlowDirection(GetFlowDirection());
Device.PlatformServices = new WindowsPlatformServices(Window.Current.Dispatcher);
Device.SetFlags(s_flags);
Device.Info = new WindowsDeviceInfo();
switch (DetectPlatform())
{
case Windows.Foundation.Metadata.Platform.Windows:
Device.SetIdiom(TargetIdiom.Desktop);
break;
case Windows.Foundation.Metadata.Platform.WindowsPhone:
Device.SetIdiom(TargetIdiom.Phone);
break;
default:
Device.SetIdiom(TargetIdiom.Tablet);
break;
}
ExpressionSearch.Default = new WindowsExpressionSearch();
Registrar.ExtraAssemblies = rendererAssemblies?.ToArray();
Registrar.RegisterAll(new[] { typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute) });
IsInitialized = true;
s_state = launchActivatedEventArgs.PreviousExecutionState;
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
Platform.UWP.Platform.SubscribeAlertsAndActionSheets();
}
static Windows.Foundation.Metadata.Platform DetectPlatform()
{
bool isHardwareButtonsAPIPresent = ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");
if (isHardwareButtonsAPIPresent)
return Windows.Foundation.Metadata.Platform.WindowsPhone;
return Windows.Foundation.Metadata.Platform.Windows;
}
static FlowDirection GetFlowDirection()
{
string resourceFlowDirection = ResourceContext.GetForCurrentView().QualifierValues["LayoutDirection"];
if (resourceFlowDirection == "LTR")
return FlowDirection.LeftToRight;
else if (resourceFlowDirection == "RTL")
return FlowDirection.RightToLeft;
return FlowDirection.MatchParent;
}
static Windows.UI.Xaml.ResourceDictionary GetTabletResources()
{
return new Windows.UI.Xaml.ResourceDictionary {
Source = new Uri("ms-appx:///Xamarin.Forms.Platform.UAP/Resources.xbf")
};
}
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();
}
}
}