Click here to Skip to main content
15,878,871 members
Articles / Silverlight
Tip/Trick

Detect Design Time Mode in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
27 Feb 2010CPOL 25.7K   2   1
In order to detect whether your application is executing in a designer you can either use the GetIsInDesignMode method of DesignerProperties,or the Dependency Property metadata directly like so:C#:bool designTime = (bool)DesignerProperties.IsInDesignModeProperty.GetMetadata( ...
In order to detect whether your application is executing in a designer you can either use the GetIsInDesignMode method of DesignerProperties,
or the Dependency Property metadata directly like so:

C#:
C#
bool designTime = (bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(
    typeof(DependencyObject)).DefaultValue

VB.NET:
VB.NET
dim designTime as Boolean = CBool(DesignerProperties.IsInDesignModeProperty.GetMetadata( _ 
GetType(DependencyObject)).DefaultValue


This can be then rolled into a static class like so:
C#:
C#
public static class DesignTimeEnvironment
{
    static bool? designTime;
    public static bool DesignTime
    {
        get
        {
            if (!designTime.HasValue)
            {
                designTime = (bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(
                      typeof(DependencyObject)).DefaultValue;
            }
            return designTime.Value;
        }
    }
}



VB.NET:
VB.NET
Public Class DesignTimeEnvironment
    Public Shared ReadOnly Property DesignTime As Boolean
        Get
            If Not DesignTimeEnvironment.designTime.HasValue Then
                DesignTimeEnvironment.designTime = New Boolean?(CBool( _ 
                   DesignerProperties.IsInDesignModeProperty.GetMetadata( _ 
GetType(DependencyObject)).DefaultValue))
            End If
            Return DesignTimeEnvironment.designTime.Value
        End Get
    End Property
    Private Shared designTime As Boolean?
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions

 
GeneralReason for my vote of 1 offff Pin
seckinkiran26-Jan-12 3:32
seckinkiran26-Jan-12 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.