2

In one of his blog posts Laurent Bugnion demonstrated the following code snippet as a means of detecting the desingtime mode for wpf

private static bool? _isInDesignMode;


/// <summary>
/// Gets a value indicating whether the control is in design mode (running in Blend
/// or Visual Studio).
/// </summary>
public static bool IsInDesignModeStatic
{
    get
    {
        if (!_isInDesignMode.HasValue)
        {
#if SILVERLIGHT
            _isInDesignMode = DesignerProperties.IsInDesignTool;
#else
            var prop = DesignerProperties.IsInDesignModeProperty;
            _isInDesignMode
                = (bool)DependencyPropertyDescriptor
                .FromProperty(prop, typeof(FrameworkElement))
                .Metadata.DefaultValue;
#endif
        }

        return _isInDesignMode.Value;
    }
}

As I happen to work in VB I set about translating this with Telerik's online code converter and ended up with the following:

Private Shared _isInDesignMode As System.Nullable(Of Boolean)

''' <summary>
''' Gets a value indicating whether the control is in design mode (running in Blend
''' or Visual Studio).
''' </summary>
Public Shared ReadOnly Property IsInDesignModeStatic() As Boolean
    Get
        If Not _isInDesignMode.HasValue Then
            #If SILVERLIGHT Then
            _isInDesignMode = DesignerProperties.IsInDesignTool
            #Else
            Dim prop = DesignerProperties.IsInDesignModeProperty
                #End If
            _isInDesignMode = CBool(DependencyPropertyDescriptor.FromProperty(prop, GetType(FrameworkElement)).Metadata.DefaultValue)
        End If

        Return _isInDesignMode.Value
    End Get
End Property

However if one has Option Strict On on (which I do by default this fails to compile pointing out that there is a discrepancy between the system.windows.DependencyProperty and the system.ComponentModel.DependencyProperty amongst other things.

Most of the errors that code converters throw up I can usually fix in the end but this one (probably because the whole wpf thing is very new to me) is causing me problems.

Could anyone explain the root cause of the error (so that I can actively understand it) and possibly provide a corrected vb conversion.

Thank you

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47

1 Answers1

1

Well the following appears to work:

Private Shared _isInDesignMode As System.Nullable(Of Boolean)

Public Shared ReadOnly Property IsInDesignMode() As Boolean
Get
    Dim prop As DependencyProperty   
    If Not _isInDesignMode.HasValue Then
        #If SILVERLIGHT Then
        _isInDesignMode = DesignerProperties.IsInDesignTool
        #Else
          prop  = DesignerProperties.IsInDesignModeProperty
            #End If
        _isInDesignMode = CBool(DependencyPropertyDescriptor.FromProperty(prop, GetType(FrameworkElement)).Metadata.DefaultValue)
    End If

    Return _isInDesignMode.Value
End Get
End Property

Two things are worth noting.

  1. Possibly there was a typo in Laurent's original blog as there is a mismatch between the private and public static declarations (although intellisense didn't seem to raise an objection to that!). Perhaps that's a C# thing, I don't know enough about C# to be able to comment on that.
  2. Option Strict On requires variable to be declared with an AS statement. prop is declared as a system.windows.DependencyProperty but is assigned as a system.cComponentModel.DependencyProperty. The compiler does not appear to object to it like this. I'm none the wiser as to why.

If any of you with a much clearer grasp of the differences between C# and VB could throw some light on this I'd be happy to know why this works, as opposed to merely accepting that it appears to work.

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47