How to avoid a quot;object reference not set to an instance of an objectquot; exception in XAML code at design time?(如何避免“未设置对象实例的对象引用设计时 XAML 代码中的异常?)
问题描述
我自己设计的 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.
这篇关于如何避免“未设置对象实例的对象引用"设计时 XAML 代码中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何避免“未设置对象实例的对象引用"设计时 XAML 代码中的异常?
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
