WPF,设计器中的“对象引用未设置为对象的实例" [英] WPF, 'Object reference not set to an instance of an object' in Designer

查看:115
本文介绍了WPF,设计器中的“对象引用未设置为对象的实例"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试为我的 XAML UserControl 重新加载设计器时,出现未将对象引用设置为对象的实例"错误.Visual Studio 将以下行突出显示为问题:

I'm getting an "Object reference not set to an instance of an object" error when I try to reload the Designer for my XAML UserControl. Visual Studio highlights the following line as being the problem:

<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"
    Width="600" TemplateData="{Binding ElementName=cbo_templates,
                               Path=SelectedItem.Data, Mode=OneWay}"/>

TemplateDetail 是另一个 UserControl.当我查看 TemplateDetail 时,它的 Designer 视图加载得很好,所以我认为那里没有问题.我的 XAML 中有一个名为 cbo_templatesComboBox,其中包含我的 Template 类的实例,该类具有 Data 属性(因此SelectedItem.Data).但是,如果我从上面的 XAML 中的 Path 中删除 .Data,我仍然会收到对象引用"错误,所以我不认为问题在于我正在尝试访问 null 上的 Path 属性.这是我的 ComboBox XAML 以防万一:

TemplateDetail is another UserControl. When I view TemplateDetail, its Designer view loads just fine, so I don't think there's a problem there. There is a ComboBox in my XAML named cbo_templates that contains instances of my Template class, which has a Data property (hence SelectedItem.Data). However, if I remove .Data from the Path in the above XAML, I still get the "Object reference" error, so I don't think the problem is that I'm trying to access the Path property on null. Here's my ComboBox XAML just in case:

<ComboBox ItemsSource="{Binding Path=List}" Grid.Row="1" Grid.Column="3"
          VerticalAlignment="Center" x:Name="cbo_templates" Width="250"
          HorizontalAlignment="Left" DisplayMemberPath="Name"
          SelectedValuePath="Name" SelectedIndex="0"/>

出现这个错误是一个真正的问题,因为设计视图不会加载,所以如果不运行应用程序,我就看不到我的 UserControl 的样子.知道有什么问题吗?它构建得很好,我在构建输出中没有看到任何绑定问题.

Getting this error is a real problem because the Design view won't load, so I can't see what my UserControl looks like without running the app. Any idea what could be wrong? It builds fine and I don't see any binding problems in the Build Output.

这里是两个UserControls的构造函数代码:

here is the constructor code for both UserControls:

带有对象引用"错误的 UserControl 构造函数:

Constructor of UserControl with "Object reference" error:

InitializeComponent();
grd_templateList.DataContext = this; // refers to containing <Grid> in XAML

UserControl 的构造函数我正在尝试嵌入,其设计视图加载正常:

Constructor of UserControl I'm trying to embed, the one whose Design view loads okay:

InitializeComponent();
grd_templateDetail.DataContext = this; // refers to containing <Grid> in XAML

我尝试在设置它们的 DataContext 属性之前在构造函数中放置一个 if (null != grd_templateList) 检查,但是没有无济于事——重新加载设计器时仍然出现对象引用"错误.

I tried putting an if (null != grd_templateList) check in the constructors before setting their DataContext properties, but that didn't help--still getting the "Object reference" error when reloading the Designer.

编辑:ComboBox 使用的 List 属性是一个 DependencyProperty.我在 Register 方法中设置了一个默认值:

the List property that the ComboBox uses is a DependencyProperty. I have a default value set in the Register method:

public static readonly DependencyProperty ListProperty =
    DependencyProperty.Register(
        "List",
        typeof(List<Template>),
        typeof(TemplateList),
        new PropertyMetadata(
            new List<Template> { _defaultTemplate }
        )
    );

即使我尝试在我的 UserControl 的构造函数中初始化 List,我在重新加载设计器时仍然会收到错误消息.我不认为问题在于 List 为 null 或 SelectedItem.Data 是一条坏路径.

Even if I try to initialize List in the constructor for my UserControl, I still get the error when reloading the Designer. I don't think the problem is that List is null or SelectedItem.Data is a bad path.

好吧,即使只是这样也会导致我的设计器无法加载,给出对象引用"错误:

okay, even just having this causes my Designer to not load, giving the "Object reference" error:

<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"
                      TemplateData="{Binding}"/>

显然,它不喜欢 TemplateData 属性被绑定.

There is something it dislikes about the TemplateData property being bound, apparently.

更神秘的是,我可以查看我的整体/主 Window 的设计视图,其中包括 UserControl设计视图给了我对象引用"错误.O_o

to add to the mystery, I can view the Design view of my overall/main Window, which includes the UserControl whose Design view gives me the "Object reference" error. O_o

推荐答案

Alex 所说的 是要走的路.但我认为理解他在说什么有点令人困惑.

What Alex says is the way to go. But I think its a little confusing to understand what he is saying.

假设您在 Visual Studio 中打开了您的项目,打开另一个 Visual Studio 实例并选择 Debug->Attach To Process.在打开的对话框中选择

Assuming you have your project open in Visual Studio, open another Visual Studio instance and select Debug->Attach To Process. In the dialog which opens select

  • XDesProc.exe(即 XAML UI 设计器),适用于 VS2012 及更新版本
  • devenv.exe 适用于较旧的 VS 版本.
  • XDesProc.exe (which is the XAML UI Designer) for VS2012 and newer or
  • devenv.exe for older VS versions.

然后为用户控件执行重新加载设计器"并查看第二个 VS 实例中的输出以检查错误究竟是什么.

Then do "Reload Designer" for the user control and see the output in the second VS instance to check what exactly is the error.

这篇关于WPF,设计器中的“对象引用未设置为对象的实例"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆