如何避免“未设置对象实例的对象引用"设计时 XAML 代码中的异常?对象、实例、异常、代码

2023-09-07 15:43:58 作者:素颜男神

我自己设计的 wpf 用户控件有问题.问题是在设计时,当我在程序中实现用户控件时,我在 XAML 代码中得到了一个 object reference not set to an instance of an object 异常.

I have a problem with a wpf usercontrol which is of my own devising. The problem is that I get a object reference not set to an instance of an object exception in XAML code at design time, when I implement the usercontrol in my program.

设计师向我展示了以下信息:

The designer showed me the following information:

at
Microsoft.Expression.Platform.InstanceBuilders.InstanceBuilderOperations.InstantiateType(Type
type, Boolean supportInternal)    at
Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateTargetType(IInstanceBuilderContext
context, ViewNode viewNode)    at
Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(IInstanceBuilderContext
context, ViewNode viewNode)    at
Microsoft.Expression.WpfPlatform.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate(IInstanceBuilderContext
context, ViewNode viewNode)    at
Microsoft.Expression.WpfPlatform.InstanceBuilders.UserControlInstanceBuilder.Instantiate(IInstanceBuilderContext
context, ViewNode viewNode)    at
Microsoft.Expression.Platform.InstanceBuilders.ViewNodeManager.CreateInstance(IInstanceBuilder
builder, ViewNode viewNode)

但我认为这些消息并没有真正的帮助......

but I think these messages are not really helpful...

如何修复或抑制此异常?

How can I fix or suppress this exception?

推荐答案

如果你在 XAML 中有 'Object reference not set to an object of an instance',但你的应用程序编译并运行良好,你通常会发现它原因是构造函数中无法在设计时解决的问题.

If you have 'Object reference not set to an instance of an object' in XAML, but your application compiles and runs fine, you will usually find out that its cause is something in a constructor that can't be resolved at design time.

您只需单击位于设计器视图底部的禁用项目代码"按钮,Visual Studio 设计器将停止尝试构造实例以提供设计时数据视图.

You can just click the "Disable project code" button located on the bottom of your designer view and Visual Studio designer will stop trying to construct an instance to provide design time data view.

有关详细信息和屏幕截图,请参阅此处.

See here for detailed information and screenshots.

 
精彩推荐
图片推荐