对象引用未设置为实例 WPF 设计器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22601770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object Reference Not set to Instance WPF Designer
提问by sriharsha KB
I have already seen the below link
我已经看过下面的链接
WPF, 'Object reference not set to an instance of an object' in Designer
My problem is similar but a little different. I am not setting any data context in XAML itself. I am creating my own user control and I am using that in another.
我的问题很相似,但有点不同。我没有在 XAML 本身中设置任何数据上下文。我正在创建自己的用户控件,并在另一个中使用它。
<usercontrols:MyControl1 x:Name="MyControl1" Grid.Row="5" Height="Auto" Width="Auto">
</usercontrols:MyControl1>
This is the code I am using in second control to refer MyControl1. This generally does not throw any exception. But, When I specify some code in the constructer of MyControl1, the exception is coming. It also says "Cannot Create an Instance of MyControl1".
这是我在第二个控件中用于引用 MyControl1 的代码。这通常不会抛出任何异常。但是,当我在 MyControl1 的构造函数中指定一些代码时,异常来了。它还说“无法创建 MyControl1 的实例”。
采纳答案by Sajeetharan
Just add this line in MainPage.xaml.cs page
只需在 MainPage.xaml.cs 页面中添加这一行
Use the static proeprty DesignerProperties.IsInDesignTool to determine if your code is currently being used in a designer tool.
使用静态属性 DesignerProperties.IsInDesignTool 确定您的代码当前是否正在设计器工具中使用。
InitializeComponent();
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Code that throws the exception
}

