Detecting design mode in Windows 8/WinRT

One of the very useful features of the MVVM Light toolkit is to help with the creation of design time data, in order to give something to see on the design surface (Expression Blend, Visual Studio designer). This is especially useful when designing list controls such as ListBox, ComboBox, etc. Without design time data, these controls will remain empty, and the designer will not see what he is working on. This can cost a lot of time and cause frustration.

Detecting design mode was always incompatible in WPF and in Silverlight.

  • In WPF, we use a rather complicated snippet:
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode
    = (bool)DependencyPropertyDescriptor
                 .FromProperty(prop, typeof(FrameworkElement))
                 .Metadata.DefaultValue;
  • In Silverlight, there is a convenient property:
_isInDesignMode = DesignerProperties.IsInDesignTool;

 

_isInDesignMode 
    = Windows.ApplicationModel.DesignMode.DesignModeEnabled;

 

Abstracting the differences

These differences can make it difficult to share code between these environments. This is why the MVVM Light toolkit abstracts these and provides a convenient property on the ViewModelBase class:

(within a ViewModel deriving from ViewModelBase):

if (IsInDesignMode)
{
    // Create design time data
}
else
{
    // Create run time data
}

(within any class not deriving from ViewModelBase):

if (ViewModelBase.IsInDesignModeStatic)
{
    // Create design time data
}
else
{
    // Create run time data
}

Happy coding!

Laurent

 

Print | posted on Sunday, September 25, 2011 9:02 PM

Feedback

# re: Detecting design mode in Windows 8/WinRT

left by Darren Mart at 10/20/2011 11:28 PM Gravatar
I've gotten more useful tips in your blog today than a dozen other sites combined. The first was the INotifyPropertyChanged debacle, now the "Design Mode" property that I (briefly) assumed was eliminated.

Despite a few growing pains I'm having in moving from Silverlight to WinRT, I'm excited about the near future, especially when I see blogs like these. Keep up the great work.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: