namespace Caliburn.Micro {
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
#if WinRT
using System.Reflection;
using Windows.UI.Core;
using Windows.UI.Xaml;
#else
using System.Windows;
using System.Windows.Threading;
using Caliburn.Micro.Core;
#endif
///
/// A implementation for the XAML platfrom.
///
public class XamlPlatformProvider : IPlatformProvider {
#if WinRT
private CoreDispatcher dispatcher;
#else
private Dispatcher dispatcher;
#endif
///
/// Initializes a new instance of the class.
///
public XamlPlatformProvider() {
#if SILVERLIGHT
dispatcher = System.Windows.Deployment.Current.Dispatcher;
#elif WinRT
dispatcher = Window.Current.Dispatcher;
#else
dispatcher = Dispatcher.CurrentDispatcher;
#endif
}
///
/// Indicates whether or not the framework is in design-time mode.
///
public bool InDesignMode {
get { return View.InDesignMode; }
}
private void ValidateDispatcher() {
if (dispatcher == null)
throw new InvalidOperationException("Not initialized with dispatcher.");
}
private bool CheckAccess() {
#if WinRT
return dispatcher == null || Window.Current != null;
#else
return dispatcher == null || dispatcher.CheckAccess();
#endif
}
///
/// Executes the action on the UI thread asynchronously.
///
/// The action to execute.
public void BeginOnUIThread(System.Action action) {
ValidateDispatcher();
#if WinRT
var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
#else
dispatcher.BeginInvoke(action);
#endif
}
///
/// Executes the action on the UI thread asynchronously.
///
/// The action to execute.
///
public Task OnUIThreadAsync(System.Action action) {
ValidateDispatcher();
#if WinRT
return dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask();
#elif NET45
return dispatcher.InvokeAsync(action).Task;
#else
var taskSource = new TaskCompletionSource