summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/Forms.cs
blob: b4435d3af561f1588a098dbd8f3577cad840c65f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Xamarin.Forms.Platform.WinPhone;

using Expression = System.Linq.Expressions.Expression;


namespace Xamarin.Forms
{
	public static class Forms
	{
		static bool s_isInitialized;

		public static UIElement ConvertPageToUIElement(this Page page, PhoneApplicationPage applicationPage)
		{
			var application = new DefaultApplication();
			application.MainPage = page;
			var result = new Platform.WinPhone.Platform(applicationPage);
			result.SetPage(page);
			return result.GetCanvas();
		}

		public static void Init()
		{
			if (s_isInitialized)
				return;

			// Needed to prevent stripping of System.Windows.Interactivity
			// which is current only referenced in the XAML DataTemplates
			var eventTrigger = new System.Windows.Interactivity.EventTrigger();

			string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;

			System.Windows.Application.Current.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary
			{
				Source = new Uri(string.Format("/{0};component/WPResources.xaml", assemblyName), UriKind.Relative)
			});

			var accent = System.Windows.Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
			System.Windows.Media.Color color = accent.Color;
			Color.Accent = Color.FromRgba(color.R, color.G, color.B, color.A);

			Log.Listeners.Add(new DelegateLogListener((c, m) => Console.WriteLine("[{0}] {1}", m, c)));

			Device.OS = TargetPlatform.WinPhone;
			Device.PlatformServices = new WP8PlatformServices();
			Device.Info = new WP8DeviceInfo();

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

			Ticker.Default = new WinPhoneTicker();

			Device.Idiom = TargetIdiom.Phone;

			ExpressionSearch.Default = new WinPhoneExpressionSearch();

			s_isInitialized = true;
		}

		class DefaultApplication : Application
		{
		}

		internal class WP8DeviceInfo : DeviceInfo
		{
			internal const string BWPorientationChangedName = "Xamarin.WP8.OrientationChanged";
			readonly double _scalingFactor;

			public WP8DeviceInfo()
			{
				MessagingCenter.Subscribe(this, BWPorientationChangedName, (FormsApplicationPage page, DeviceOrientation orientation) => { CurrentOrientation = orientation; });

				Content content = System.Windows.Application.Current.Host.Content;

				// Scaling Factor for Windows Phone 8 is relative to WVGA: https://msdn.microsoft.com/en-us/library/windows/apps/jj206974(v=vs.105).aspx
				_scalingFactor = content.ScaleFactor / 100d;

				PixelScreenSize = new Size(content.ActualWidth * _scalingFactor, content.ActualHeight * _scalingFactor);
				ScaledScreenSize = new Size(content.ActualWidth, content.ActualHeight);
			}

			public override Size PixelScreenSize { get; }

			public override Size ScaledScreenSize { get; }

			public override double ScalingFactor
			{
				get { return _scalingFactor; }
			}

			protected override void Dispose(bool disposing)
			{
				MessagingCenter.Unsubscribe<FormsApplicationPage, DeviceOrientation>(this, BWPorientationChangedName);
				base.Dispose(disposing);
			}
		}

		sealed class WinPhoneExpressionSearch : IExpressionSearch
		{
			List<object> _results;
			Type _targeType;

			public List<T> FindObjects<T>(Expression expression) where T : class
			{
				_results = new List<object>();
				_targeType = typeof(T);

				Visit(expression);

				return _results.Select(o => o as T).ToList();
			}

			void Visit(Expression expression)
			{
				if (expression == null)
					return;

				switch (expression.NodeType)
				{
					case ExpressionType.Negate:
					case ExpressionType.NegateChecked:
					case ExpressionType.Not:
					case ExpressionType.Convert:
					case ExpressionType.ConvertChecked:
					case ExpressionType.ArrayLength:
					case ExpressionType.Quote:
					case ExpressionType.TypeAs:
					case ExpressionType.UnaryPlus:
						Visit(((UnaryExpression)expression).Operand);
						break;
					case ExpressionType.Add:
					case ExpressionType.AddChecked:
					case ExpressionType.Subtract:
					case ExpressionType.SubtractChecked:
					case ExpressionType.Multiply:
					case ExpressionType.MultiplyChecked:
					case ExpressionType.Divide:
					case ExpressionType.Modulo:
					case ExpressionType.Power:
					case ExpressionType.And:
					case ExpressionType.AndAlso:
					case ExpressionType.Or:
					case ExpressionType.OrElse:
					case ExpressionType.LessThan:
					case ExpressionType.LessThanOrEqual:
					case ExpressionType.GreaterThan:
					case ExpressionType.GreaterThanOrEqual:
					case ExpressionType.Equal:
					case ExpressionType.NotEqual:
					case ExpressionType.Coalesce:
					case ExpressionType.ArrayIndex:
					case ExpressionType.RightShift:
					case ExpressionType.LeftShift:
					case ExpressionType.ExclusiveOr:
						var binary = (BinaryExpression)expression;
						Visit(binary.Left);
						Visit(binary.Right);
						Visit(binary.Conversion);
						break;
					case ExpressionType.TypeIs:
						Visit(((TypeBinaryExpression)expression).Expression);
						break;
					case ExpressionType.Conditional:
						var conditional = (ConditionalExpression)expression;
						Visit(conditional.Test);
						Visit(conditional.IfTrue);
						Visit(conditional.IfFalse);
						break;
					case ExpressionType.MemberAccess:
						VisitMemberAccess((MemberExpression)expression);
						break;
					case ExpressionType.Call:
						var methodCall = (MethodCallExpression)expression;
						Visit(methodCall.Object);
						VisitList(methodCall.Arguments, Visit);
						break;
					case ExpressionType.Lambda:
						Visit(((LambdaExpression)expression).Body);
						break;
					case ExpressionType.New:
						VisitList(((NewExpression)expression).Arguments, Visit);
						break;
					case ExpressionType.NewArrayInit:
					case ExpressionType.NewArrayBounds:
						VisitList(((NewArrayExpression)expression).Expressions, Visit);
						break;
					case ExpressionType.Invoke:
						var invocation = (InvocationExpression)expression;
						VisitList(invocation.Arguments, Visit);
						Visit(invocation.Expression);
						break;
					case ExpressionType.MemberInit:
						var init = (MemberInitExpression)expression;
						VisitList(init.NewExpression.Arguments, Visit);
						VisitList(init.Bindings, VisitBinding);
						break;
					case ExpressionType.ListInit:
						var init1 = (ListInitExpression)expression;
						VisitList(init1.NewExpression.Arguments, Visit);
						VisitList(init1.Initializers, initializer => VisitList(initializer.Arguments, Visit));
						break;
					case ExpressionType.Constant:
						break;
					default:
						throw new ArgumentException(string.Format("Unhandled expression type: '{0}'", expression.NodeType));
				}
			}

			void VisitBinding(MemberBinding binding)
			{
				switch (binding.BindingType)
				{
					case MemberBindingType.Assignment:
						Visit(((MemberAssignment)binding).Expression);
						break;
					case MemberBindingType.MemberBinding:
						VisitList(((MemberMemberBinding)binding).Bindings, VisitBinding);
						break;
					case MemberBindingType.ListBinding:
						VisitList(((MemberListBinding)binding).Initializers, initializer => VisitList(initializer.Arguments, Visit));
						break;
					default:
						throw new ArgumentException(string.Format("Unhandled binding type '{0}'", binding.BindingType));
				}
			}

			static void VisitList<TList>(IEnumerable<TList> list, Action<TList> visitor)
			{
				foreach (TList element in list)
					visitor(element);
			}

			void VisitMemberAccess(MemberExpression member)
			{
				if (member.Expression is ConstantExpression && member.Member is FieldInfo)
				{
					object container = ((ConstantExpression)member.Expression).Value;
					object value = ((FieldInfo)member.Member).GetValue(container);

					if (_targeType.IsInstanceOfType(value))
						_results.Add(value);
				}
				Visit(member.Expression);
			}
		}
	}
}