summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 20:02:25 (GMT)
committerJason Smith <jason.smith@xamarin.com>2016-03-22 23:13:41 (GMT)
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs')
-rw-r--r--Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs232
1 files changed, 232 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs b/Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs
new file mode 100644
index 0000000..c20071f
--- /dev/null
+++ b/Xamarin.Forms.Platform.WP8/WP8PlatformServices.cs
@@ -0,0 +1,232 @@
+using System;
+using System.IO;
+using System.IO.IsolatedStorage;
+using System.Net;
+using System.Reflection;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Threading;
+using Windows.System;
+using Xamarin.Forms.Platform.WinPhone;
+
+namespace Xamarin.Forms
+{
+ internal class WP8PlatformServices : IPlatformServices
+ {
+ static readonly MD5CryptoServiceProvider Checksum = new MD5CryptoServiceProvider();
+
+ public void BeginInvokeOnMainThread(Action action)
+ {
+ Deployment.Current.Dispatcher.BeginInvoke(action);
+ }
+
+ public ITimer CreateTimer(Action<object> callback)
+ {
+ return new _Timer(new Timer(o => callback(o)));
+ }
+
+ public ITimer CreateTimer(Action<object> callback, object state, int dueTime, int period)
+ {
+ return new _Timer(new Timer(o => callback(o), state, dueTime, period));
+ }
+
+ public ITimer CreateTimer(Action<object> callback, object state, long dueTime, long period)
+ {
+ return new _Timer(new Timer(o => callback(o), state, dueTime, period));
+ }
+
+ public ITimer CreateTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)
+ {
+ return new _Timer(new Timer(o => callback(o), state, dueTime, period));
+ }
+
+ public ITimer CreateTimer(Action<object> callback, object state, uint dueTime, uint period)
+ {
+ return new _Timer(new Timer(o => callback(o), state, dueTime, period));
+ }
+
+ public Assembly[] GetAssemblies()
+ {
+ return AppDomain.CurrentDomain.GetAssemblies();
+ }
+
+ public string GetMD5Hash(string input)
+ {
+ byte[] bytes = Checksum.ComputeHash(Encoding.UTF8.GetBytes(input));
+ var ret = new char[32];
+ for (var i = 0; i < 16; i++)
+ {
+ ret[i * 2] = (char)Hex(bytes[i] >> 4);
+ ret[i * 2 + 1] = (char)Hex(bytes[i] & 0xf);
+ }
+ return new string(ret);
+ }
+
+ 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["PhoneFontSizeNormal"];
+ return (double)System.Windows.Application.Current.Resources["PhoneFontSizeMedium"];
+ case NamedSize.Micro:
+ return (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"] - 3;
+ case NamedSize.Small:
+ return (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"];
+ case NamedSize.Medium:
+ if (useOldSizes)
+ goto case NamedSize.Default;
+ return (double)System.Windows.Application.Current.Resources["PhoneFontSizeMedium"];
+ case NamedSize.Large:
+ return (double)System.Windows.Application.Current.Resources["PhoneFontSizeLarge"];
+ 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.AllowReadStreamBuffering = true;
+ 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 _IsolatedStorageFile(IsolatedStorageFile.GetUserStoreForApplication());
+ }
+
+ public bool IsInvokeRequired
+ {
+ get { return !Deployment.Current.Dispatcher.CheckAccess(); }
+ }
+
+ public void OpenUriAction(Uri uri)
+ {
+ Launcher.LaunchUriAsync(uri);
+ }
+
+ 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();
+ };
+ }
+
+ static int Hex(int v)
+ {
+ if (v < 10)
+ return '0' + v;
+ return 'a' + v - 10;
+ }
+
+ public class _Timer : ITimer
+ {
+ readonly Timer _timer;
+
+ public _Timer(Timer timer)
+ {
+ _timer = timer;
+ }
+
+ public void Change(int dueTime, int period)
+ {
+ _timer.Change(dueTime, period);
+ }
+
+ public void Change(long dueTime, long period)
+ {
+ _timer.Change(dueTime, period);
+ }
+
+ public void Change(TimeSpan dueTime, TimeSpan period)
+ {
+ _timer.Change(dueTime, period);
+ }
+
+ public void Change(uint dueTime, uint period)
+ {
+ _timer.Change(dueTime, period);
+ }
+ }
+
+ public class _IsolatedStorageFile : IIsolatedStorageFile
+ {
+ readonly IsolatedStorageFile _isolatedStorageFile;
+
+ public _IsolatedStorageFile(IsolatedStorageFile isolatedStorageFile)
+ {
+ _isolatedStorageFile = isolatedStorageFile;
+ }
+
+ public Task CreateDirectoryAsync(string path)
+ {
+ _isolatedStorageFile.CreateDirectory(path);
+ return Task.FromResult(true);
+ }
+
+ public Task<bool> GetDirectoryExistsAsync(string path)
+ {
+ return Task.FromResult(_isolatedStorageFile.DirectoryExists(path));
+ }
+
+ public Task<bool> GetFileExistsAsync(string path)
+ {
+ return Task.FromResult(_isolatedStorageFile.FileExists(path));
+ }
+
+ public Task<DateTimeOffset> GetLastWriteTimeAsync(string path)
+ {
+ return Task.FromResult(_isolatedStorageFile.GetLastWriteTime(path));
+ }
+
+ public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access)
+ {
+ Stream stream = _isolatedStorageFile.OpenFile(path, (System.IO.FileMode)mode, (System.IO.FileAccess)access);
+ return Task.FromResult(stream);
+ }
+
+ public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share)
+ {
+ Stream stream = _isolatedStorageFile.OpenFile(path, (System.IO.FileMode)mode, (System.IO.FileAccess)access, (System.IO.FileShare)share);
+ return Task.FromResult(stream);
+ }
+ }
+ }
+} \ No newline at end of file