在他的一篇博文中,Laurent Bugnion 演示了以下代码片段作为检测 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;
}
}
当我碰巧在 VB 中工作时,我开始使用 Telerik 的在线代码转换器进行翻译,结果如下:
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
但是,如果一个人启用了 Option Strict On(默认情况下我这样做,这将无法编译,指出 system.windows.DependencyProperty 和 system.ComponentModel.DependencyProperty 之间存在差异。
代码转换器抛出的大多数错误我通常可以最终修复,但是这个(可能是因为整个 wpf 对我来说很新)给我带来了问题。
任何人都可以解释错误的根本原因(以便我可以积极理解它)并可能提供更正的 vb 转换。
谢谢