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;
- In WinRT, we have to use yet another property:
_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