forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWPFPlatformServices.cs
154 lines (135 loc) · 3.97 KB
/
WPFPlatformServices.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.WPF
{
public class WPFPlatformServices : IPlatformServices
{
public bool IsInvokeRequired
{
get { return !System.Windows.Application.Current.Dispatcher.CheckAccess(); }
}
public string RuntimePlatform => Device.WPF;
public void OpenUriAction(Uri uri)
{
System.Diagnostics.Process.Start(uri.AbsoluteUri);
}
public void BeginInvokeOnMainThread(Action action)
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(action);
}
public Ticker CreateTicker()
{
return new WPFTicker();
}
public Assembly[] GetAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
public string GetMD5Hash(string input)
{
// MSDN - Documentation -https://msdn.microsoft.com/en-us/library/system.security.cryptography.md5(v=vs.110).aspx
using (MD5 md5Hash = MD5.Create())
{
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
{
switch (size)
{
case NamedSize.Default:
if (typeof(Label).IsAssignableFrom(targetElementType))
return (double)System.Windows.Application.Current.Resources["FontSizeNormal"];
return (double)System.Windows.Application.Current.Resources["FontSizeMedium"];
case NamedSize.Micro:
return (double)System.Windows.Application.Current.Resources["FontSizeSmall"] - 3;
case NamedSize.Small:
return (double)System.Windows.Application.Current.Resources["FontSizeSmall"];
case NamedSize.Medium:
if (useOldSizes)
goto case NamedSize.Default;
return (double)System.Windows.Application.Current.Resources["FontSizeMedium"];
case NamedSize.Large:
return (double)System.Windows.Application.Current.Resources["FontSizeLarge"];
default:
throw new ArgumentOutOfRangeException("size");
}
}
public Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<Stream>();
try
{
HttpWebRequest request = WebRequest.CreateHttp(uri);
request.BeginGetResponse(ar =>
{
if (cancellationToken.IsCancellationRequested)
{
tcs.SetCanceled();
return;
}
try
{
Stream stream = request.EndGetResponse(ar).GetResponseStream();
tcs.TrySetResult(stream);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}, null);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
return tcs.Task;
}
public IIsolatedStorageFile GetUserStoreForApplication()
{
return new WPFIsolatedStorageFile(IsolatedStorageFile.GetUserStoreForAssembly());
}
public void StartTimer(TimeSpan interval, Func<bool> callback)
{
var timer = new DispatcherTimer { Interval = interval };
timer.Start();
timer.Tick += (sender, args) =>
{
bool result = callback();
if (!result)
timer.Stop();
};
}
public void QuitApplication()
{
System.Windows.Application.Current.Shutdown();
}
public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
return Platform.GetNativeSize(view, widthConstraint, heightConstraint);
}
}
}