Skip to content

Commit

Permalink
add View.InDesignMode property
Browse files Browse the repository at this point in the history
  • Loading branch information
tibel committed Aug 8, 2014
1 parent a96c0f6 commit 64e781d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Caliburn.Micro.Platform/ActionMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public AttachedCollection<Parameter> Parameters {
/// </summary>
#if WinRT81
protected override void OnAttached() {
if (!Caliburn.Micro.Execute.InDesignMode) {
if (!View.InDesignMode) {
Parameters.Attach(AssociatedObject);
Parameters.OfType<Parameter>().Apply(x => x.MakeAwareOf(this));

Expand All @@ -160,7 +160,7 @@ void ElementUnloaded(object sender, RoutedEventArgs e)
}
#else
protected override void OnAttached() {
if (!Execute.InDesignMode) {
if (!View.InDesignMode) {
Parameters.Attach(AssociatedObject);
Parameters.Apply(x => x.MakeAwareOf(this));

Expand All @@ -184,7 +184,7 @@ static void HandlerPropertyChanged(DependencyObject d, DependencyPropertyChanged
/// Called when the action is being detached from its AssociatedObject, but before it has actually occurred.
/// </summary>
protected override void OnDetaching() {
if (!Caliburn.Micro.Execute.InDesignMode) {
if (!View.InDesignMode) {
Detaching(this, EventArgs.Empty);
AssociatedObject.Loaded -= ElementLoaded;
Parameters.Detach();
Expand Down
8 changes: 4 additions & 4 deletions src/Caliburn.Micro.Platform/Bind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void SetModel(DependencyObject dependencyObject, object value) {
}

static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (Execute.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) {
if (View.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) {
return;
}

Expand All @@ -105,7 +105,7 @@ static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs
}

static void ModelWithoutContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (Execute.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) {
if (View.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) {
return;
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public static void SetAtDesignTime(DependencyObject dependencyObject, bool value
}

static void AtDesignTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (!Execute.InDesignMode)
if (!View.InDesignMode)
return;

var atDesignTime = (bool) e.NewValue;
Expand All @@ -184,7 +184,7 @@ static void AtDesignTimeChanged(DependencyObject d, DependencyPropertyChangedEve
);

static void DataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (!Execute.InDesignMode)
if (!View.InDesignMode)
return;

var enable = d.GetValue(AtDesignTimeProperty);
Expand Down
27 changes: 27 additions & 0 deletions src/Caliburn.Micro.Platform/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
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;
#else
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
Expand Down Expand Up @@ -358,5 +360,30 @@ static void SetContentPropertyCore(object targetLocation, object view) {
}
}
#endif

private static bool? inDesignMode;

/// <summary>
/// Gets a value that indicates whether the process is running in design mode.
/// </summary>
public static bool InDesignMode
{
get
{
if (inDesignMode == null)
{
#if WinRT
inDesignMode = DesignMode.DesignModeEnabled;
#elif SILVERLIGHT
inDesignMode = DesignerProperties.IsInDesignTool;
#else
var descriptor = DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement));
inDesignMode = (bool)descriptor.Metadata.DefaultValue;
#endif
}

return inDesignMode.GetValueOrDefault(false);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Platform/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public static void AddSubNamespaceMapping(string nsSource, string nsTarget, stri
public static Func<Type, DependencyObject, object, Type> LocateTypeForModelType = (modelType, displayLocation, context) => {
var viewTypeName = modelType.FullName;

if (Execute.InDesignMode) {
if (View.InDesignMode) {
viewTypeName = ModifyModelTypeAtDesignTime(viewTypeName);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Platform/ViewModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static bool ShouldApplyConventions(FrameworkElement view) {
#if !WinRT
// when using d:DesignInstance, Blend tries to assign the DesignInstanceExtension class as the DataContext,
// so here we get the actual ViewModel which is in the Instance property of DesignInstanceExtension
if (Execute.InDesignMode) {
if (View.InDesignMode) {
var vmType = viewModel.GetType();
if (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension") {
var propInfo = vmType.GetProperty("Instance", BindingFlags.Instance | BindingFlags.NonPublic);
Expand Down
20 changes: 2 additions & 18 deletions src/Caliburn.Micro.Platform/XamlPlatformProvider.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
namespace Caliburn.Micro {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
#if WinRT
using System.Reflection;
using Windows.ApplicationModel;
using Windows.UI.Core;
using Windows.UI.Xaml;
#else
using System.Windows;
using System.Windows.Threading;
#endif

/// <summary>
/// A <see cref="IPlatformProvider"/> implementation for the XAML platfrom.
/// </summary>
public class XamlPlatformProvider : IPlatformProvider {
private bool? inDesignMode;
#if WinRT
private CoreDispatcher dispatcher;
#else
Expand All @@ -41,20 +38,7 @@ public XamlPlatformProvider() {
/// Indicates whether or not the framework is in design-time mode.
/// </summary>
public bool InDesignMode {
get {
if (inDesignMode == null) {
#if WinRT
inDesignMode = DesignMode.DesignModeEnabled;
#elif SILVERLIGHT
inDesignMode = DesignerProperties.IsInDesignTool;
#else
var descriptor = DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof (FrameworkElement));
inDesignMode = (bool)descriptor.Metadata.DefaultValue;
#endif
}

return inDesignMode.GetValueOrDefault(false);
}
get { return View.InDesignMode; }
}

private void ValidateDispatcher() {
Expand Down

0 comments on commit 64e781d

Please sign in to comment.