#if XFORMS
namespace Caliburn.Micro.Core.Xamarin.Forms
#else
namespace Caliburn.Micro
#endif
{
using System;
using System.Linq;
#if WinRT
using System.Reflection;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
#elif XFORMS
using System.Reflection;
using global::Xamarin.Forms;
using UIElement = global::Xamarin.Forms.Element;
using FrameworkElement = global::Xamarin.Forms.VisualElement;
using DependencyProperty = global::Xamarin.Forms.BindableProperty;
using DependencyObject = global::Xamarin.Forms.BindableObject;
using ContentControl = global::Xamarin.Forms.ContentView;
#else
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Caliburn.Micro.Core;
#endif
///
/// Hosts attached properties related to view models.
///
public static class View {
static readonly ILog Log = LogManager.GetLog(typeof(View));
#if WinRT || XFORMS
const string DefaultContentPropertyName = "Content";
#else
static readonly ContentPropertyAttribute DefaultContentProperty = new ContentPropertyAttribute("Content");
#endif
///
/// A dependency property which allows the framework to track whether a certain element has already been loaded in certain scenarios.
///
public static readonly DependencyProperty IsLoadedProperty =
DependencyPropertyHelper.RegisterAttached(
"IsLoaded",
typeof(bool),
typeof(View),
false
);
///
/// A dependency property which marks an element as a name scope root.
///
public static readonly DependencyProperty IsScopeRootProperty =
DependencyPropertyHelper.RegisterAttached(
"IsScopeRoot",
typeof(bool),
typeof(View),
false
);
///
/// A dependency property which allows the override of convention application behavior.
///
public static readonly DependencyProperty ApplyConventionsProperty =
DependencyPropertyHelper.RegisterAttached(
"ApplyConventions",
typeof(bool?),
typeof(View)
);
///
/// A dependency property for assigning a context to a particular portion of the UI.
///
public static readonly DependencyProperty ContextProperty =
DependencyPropertyHelper.RegisterAttached(
"Context",
typeof(object),
typeof(View),
null,
OnContextChanged
);
///
/// A dependency property for attaching a model to the UI.
///
public static DependencyProperty ModelProperty =
DependencyPropertyHelper.RegisterAttached(
"Model",
typeof(object),
typeof(View),
null,
OnModelChanged
);
///
/// Used by the framework to indicate that this element was generated.
///
public static readonly DependencyProperty IsGeneratedProperty =
DependencyPropertyHelper.RegisterAttached(
"IsGenerated",
typeof(bool),
typeof(View),
false
);
///
/// Executes the handler immediately if the element is loaded, otherwise wires it to the Loaded event.
///
/// The element.
/// The handler.
/// true if the handler was executed immediately; false otherwise
public static bool ExecuteOnLoad(FrameworkElement element, RoutedEventHandler handler) {
#if XFORMS
handler(element, new RoutedEventArgs());
return true;
#else
#if SILVERLIGHT
if ((bool)element.GetValue(IsLoadedProperty)) {
#elif WinRT
if (IsElementLoaded(element)) {
#else
if(element.IsLoaded) {
#endif
handler(element, new RoutedEventArgs());
return true;
}
RoutedEventHandler loaded = null;
loaded = (s, e) => {
element.Loaded -= loaded;
#if SILVERLIGHT
element.SetValue(IsLoadedProperty, true);
#endif
handler(s, e);
};
element.Loaded += loaded;
return false;
#endif
}
///
/// Executes the handler when the element is unloaded.
///
/// The element.
/// The handler.
public static void ExecuteOnUnload(FrameworkElement element, RoutedEventHandler handler) {
#if !XFORMS
RoutedEventHandler unloaded = null;
unloaded = (s, e) => {
element.Unloaded -= unloaded;
handler(s, e);
};
element.Unloaded += unloaded;
#endif
}
#if WinRT
///
/// Determines whether the specified is loaded.
///
/// The element.
/// true if the element is loaded; otherwise, false.
///
public static bool IsElementLoaded(FrameworkElement element) {
try
{
if ((element.Parent ?? VisualTreeHelper.GetParent(element)) != null)
{
return true;
}
var rootVisual = Window.Current.Content;
if (rootVisual != null)
{
return element == rootVisual;
}
return false;
}
catch
{
return false;
}
}
#endif
///
/// Executes the handler the next time the elements's LayoutUpdated event fires.
///
/// The element.
/// The handler.
#if WinRT
public static void ExecuteOnLayoutUpdated(FrameworkElement element, EventHandler