【问题标题】:WPF Designer Binding with Converter throwing Object Reference not set to instance of an objectWPF 设计器绑定与转换器抛出对象引用未设置为对象的实例
【发布时间】:2016-04-29 16:29:10
【问题描述】:

我的 XAML 设计器在绑定时使用我的转换器之一的任何地方都显示对象引用未设置为实例错误。经过大量挖掘后,我发现它发生在这一特定行,因为它无法在设计时找到获取值。

TransactionViewModel.getInstance()

即使我试图通过检查它不为空来避免它,它仍然会抛出对象引用错误

TransactionViewModel 是一个单例类,带有“getInstance()”函数来获取实例。

这是我的转换函数如下

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
    Dim FieldName As String = value.ToString
    Dim FieldNum As Integer = Integer.Parse(parameter)

    If TransactionViewModel.getInstance IsNot Nothing Then
        If TransactionViewModel.getInstance._NR IsNot Nothing Then
            If TransactionViewModel.getInstance._NR.ContainsField(FieldNum, FieldName) Then
                Return Visibility.Visible
            Else
                Return Visibility.Hidden
            End If
        End If
    End If
    Return Visibility.Visible
End Function

有什么建议吗?

【问题讨论】:

    标签: wpf vb.net xaml


    【解决方案1】:

    请尝试一下它可能对你有帮助

    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        //Code that throws the exception
       //Code that return null
    }
    

    VB:

    If Not System.ComponentModel.DesignerProperties.GetIsInDesignMode(New DependencyObject) Then
            'Code that throws the ex
            'Code that return null
    End If
    

    【讨论】:

    • 这行得通; If Not System.ComponentModel.DesignerProperties.GetIsInDesignMode(New DependencyObject) Then 'Code that throws the ex 'Code that return null End If 谢谢
    • 也谢谢你,我很高兴能帮上忙
    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 2012-04-22
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多