Published articles on other web sites*

Published articles on other web sites*

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

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...