【问题标题】:WPF Dependency property crashes in GUI by using my interface使用我的界面在 GUI 中的 WPF 依赖项属性崩溃
【发布时间】:2011-08-13 08:00:22
【问题描述】:

我的 UserControl 工作正常。今天我为用户控件添加了一个简单的界面。现在我可以在设计模式 (XAML) 中加载它,但是在新的 Window1.Xaml 中使用 UserControl 时它会崩溃。有什么想法吗?

'在匹配指定绑定约束的类型'OUTPUT___WPF01.ucUpDownBox'上调用构造函数引发异常。

''' <summary>
''' BorderColor Dependency Property
''' </summary>
Public Shared ReadOnly BorderColorProperty As DependencyProperty = _
    DependencyProperty.Register("BorderColor", GetType(System.Windows.Media.Color), GetType(ucUpDownBox), _
        New FrameworkPropertyMetadata(System.Windows.Media.Color.FromArgb(255, 50, 50, 50), _
            FrameworkPropertyMetadataOptions.None, _
            AddressOf OnBorderColorChanged))

''' <summary>
''' Gets or sets the BorderColor property.  This dependency property 
''' indicates ....
''' </summary>
Public Property BorderColor() As System.Windows.Media.Color Implements IButton.Color '<--- IButton.Color is the interface with Media.Color
    Get
        Return CType(GetValue(BorderColorProperty), System.Windows.Media.Color)
    End Get
    Set(ByVal value As System.Windows.Media.Color)
        SetValue(BorderColorProperty, value)
    End Set
End Property

''' <summary>
''' Handles changes to the BorderColor property.
''' </summary>
Private Overloads Shared Sub OnBorderColorChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    Dim target As ucUpDownBox = CType(d, ucUpDownBox)
    Dim oldBorderColor As System.Windows.Media.Color = CType(e.OldValue, System.Windows.Media.Color)
    Dim newBorderColor As System.Windows.Media.Color = target.BorderColor
    target.OnBorderColorChanged(oldBorderColor, newBorderColor)
End Sub

''' <summary>
''' Provides derived classes an opportunity to handle changes to the BorderColor property.
''' </summary>
Protected Overridable Overloads Sub OnBorderColorChanged(ByVal oldBorderColor As System.Windows.Media.Color, ByVal newBorderColor As System.Windows.Media.Color)
    Dim uc As ucUpDownBox = CType(Me, ucUpDownBox)

    uc.Col1A.Color = AddLuminance(BorderColor, 60)
    uc.Col1B.Color = AddLuminance(BorderColor, -60)
End Sub

【问题讨论】:

    标签: wpf vb.net properties interface dependencies


    【解决方案1】:

    这通常发生在默认值的类型与依赖属性的类型不同时。

    例如0 而不是 0.0 代表双精度,或颜色而不是画笔。

    也可能是由于在构造函数中使用非赋值(null)属性引起的。

    【讨论】:

    • 这解决了我的问题,非常感谢! new PropertyMetadata(string.Empty)new PropertyMetadata(0)。如您所说,它必须是类型的默认值。
    【解决方案2】:

    “对匹配指定绑定约束的类型...调用构造函数引发异常”

    此错误通常表示解析 xaml 代码时出现运行时错误。这当然不会告诉你太多。因此,您需要获取更多信息。基本上,您看到的异常只是一般错误,我们想看看是什么导致了这个问题。

    要获得真正的异常(或我们所说的“内部异常”): - 激活对 Visual Studio 中抛出的所有异常的捕获。 打开 Debug > Exceptions 并激活 “Common Language Runtime Exceptions” 的“throw”列中的复选框。

    现在再次触发崩溃,看看你会得到什么。理想情况下,我们希望看到堆栈跟踪。 如果您查看跟踪,您可能会看到代码的某些部分被调用。看看这段代码,看看你能不能从那里找出问题。

    如果您仍然卡住,请在您的问题中发布一些代码和堆栈跟踪,我们将看看我们可以从那里做些什么。

    有时“输出”窗口也会为您提供一些信息。

    【讨论】:

    • 嗨,丽兹。非常感谢你。现在,我的应用程序中有 101 个错误需要解决。这将需要很多时间。听起来很糟糕?我喜欢在他们的心里解决这些问题。感谢您的提示。我希望通过这种方式,我会找到解决方案。
    猜你喜欢
    • 2011-03-31
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2012-11-27
    • 2011-07-22
    • 2013-01-08
    相关资源
    最近更新 更多