C# DependencyProperty导致Visual Studio在设计时崩溃

C# DependencyProperty导致Visual Studio在设计时崩溃,c#,wpf,visual-studio,crash,dependency-properties,C#,Wpf,Visual Studio,Crash,Dependency Properties,在VisualStudio中,当试图通过设计模式查看自定义控件的属性时,VisualStudio会在我看到属性之前崩溃 自从在我的控件中实现依赖项属性以来,这种更改就开始了 为什么会发生这种情况(或是什么原因造成的)?正确设置依赖项属性需要什么? 注意:自定义控件位于单独的.DLL中,我无法将Visual Studio配置为在工具箱中显示自定义控件。但是,VisualStudio知道该控件存在,因为它在手动键入XAML时显示为建议 还请注意,我可以查看默认的公共属性。展开其他属性时会出现此问题

在VisualStudio中,当试图通过设计模式查看自定义控件的属性时,VisualStudio会在我看到属性之前崩溃

自从在我的控件中实现依赖项属性以来,这种更改就开始了

为什么会发生这种情况(或是什么原因造成的)?正确设置依赖项属性需要什么?

注意:自定义控件位于单独的.DLL中,我无法将Visual Studio配置为在工具箱中显示自定义控件。但是,VisualStudio知道该控件存在,因为它在手动键入XAML时显示为建议

还请注意,我可以查看默认的公共属性。展开其他属性时会出现此问题

以下是我设置的依赖项属性:

public static readonly DependencyProperty ThicknessProperty =
    DependencyProperty.Register("Thickness", typeof(double), typeof(LineTypeViewer), new FrameworkPropertyMetadata(1, OnPropertyChanged));

public static readonly DependencyProperty ForegroundProperty =
    DependencyProperty.Register("Foreground", typeof(SolidColorBrush), typeof(LineTypeViewer), new FrameworkPropertyMetadata(Brushes.Black, OnPropertyChanged));

public static readonly DependencyProperty CurrentLineTypeProperty =
    DependencyProperty.Register("CurrentLineType", typeof(LineType), typeof(LineTypeViewer), new FrameworkPropertyMetadata(null, OnPropertyChanged));

public static readonly DependencyProperty DrawingWidthProperty =
    DependencyProperty.Register("DrawingWidth", typeof(float), typeof(LineTypeViewer), new FrameworkPropertyMetadata(5, OnPropertyChanged, OnCoerceDrawingWidthProperty));

public static readonly DependencyProperty MinRepetitionsProperty =
    DependencyProperty.Register("MinRepetitions", typeof(int), typeof(LineTypeViewer), new FrameworkPropertyMetadata(3, OnPropertyChanged));
还有。。。由于它与上述属性中描述的元数据有关,因此我使用以下回调函数:

private static void OnPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{   //Update the layout when any of the above properties are changed.
    LineTypeViewer viewer = (LineTypeViewer)source;
    viewer.UpdateLayout();
}

private static object OnCoerceDrawingWidthProperty(DependencyObject source, object data)
{
    if (data is float) //The DrawingWidth must be a float.
    {
        float dw = (float)data;
        if (dw < 0) return 5; //The DrawingWidth cannot be negative.
        else return dw;
    }
    else return 5;
}
 
根据我一直在使用的指南(&),我的代码看起来是正确的,所以依赖项属性可能没有问题。我怀疑这是因为依赖项属性允许您在VisualStudio中通过设计时自定义控件。奇怪的是,我不能像前面提到的那样扩展其他属性


非常感谢您的帮助!谢谢。:)

FrameworkPropertyMetadata
无法为属性的默认值强制执行正确的类型,这意味着您需要确保提供准确的预期类型。e、 g

DependencyProperty.Register
(
    "Thickness",
    typeof(double),
    typeof(LineTypeViewer),
    new FrameworkPropertyMetadata(1, OnPropertyChanged)
);
这里为
double
属性提供
int
。您需要将
1
更改为
1.0
或将其转换为
(双精度)1


这可能是崩溃的原因,也可能不是,但它会导致运行时错误。

我认为您不希望在每个OnPropertyChanged中调用UpdateLayout()。特别是它说“应该避免在元素树的每次增量和微小更改后调用UpdateLayout”的地方,我想知道这是否是某种递归。这实际上也可能在设计器之外失败,具体取决于您将其放入的容器类型。它可能在网格中工作,但在堆栈面板或具有动态大小的东西中来回摆动。有趣的一点。你让我注意到我的代码有问题。我应该调用
UpdateView
,这是我自己的方法。不幸的是,在修复、重建等之后。。。我还有同样的问题。我确实注意到,当我在工具箱中显示所有控件时,我的自定义控件显示为灰色。嘿,就是这样!多么简单!:)
DependencyProperty.Register
(
    "Thickness",
    typeof(double),
    typeof(LineTypeViewer),
    new FrameworkPropertyMetadata(1, OnPropertyChanged)
);