WPF 设计时错误对象引用未设置为对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12578463/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:31:54  来源:igfitidea点击:

WPF design time error Object reference not set to an instance of an object

wpfxamldependency-propertiesobservablecollectiondesign-time

提问by Micha? Jankowski

Ok I am getting sick of this problem.

好吧,我厌倦了这个问题。

I am trying to make UserControl where I can populate its content from XAML. Previously I have created ObservableCollection DependencyProperty. It was working at runtime but at design time there was an error:

我正在尝试使 UserControl 可以从 XAML 填充其内容。之前我已经创建了 ObservableCollection DependencyProperty。它在运行时工作,但在设计时出现错误:

Object reference not set to an instance of an object.

你调用的对象是空的。

Now I did simpler version:

现在我做了更简单的版本:

public partial class UC : UserControl
{
    public UC()
    {
        Labels = new ObservableCollection<Label>();
        InitializeComponent();

        Labels.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Labels_CollectionChanged);
    }

    void Labels_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        foreach (Label uc in e.NewItems)
            Container.Children.Add(uc);
    }

    private ObservableCollection<Label> _lables = new ObservableCollection<Label>();

    public ObservableCollection<Label> Labels
    {
        get { return _lables; }
        set { _lables = value; }
    }
}

This is how I like to use my UserControll

这就是我喜欢使用我的 UserControll 的方式

<Window x:Class="WpfApplication1.MainWindow"
    xmlns:gsh="clr-namespace:WpfApplication1"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,0,30">
    <gsh:UC x:Name="uC1">
        <gsh:UC.Labels>
            <Label Content="qwerty1" />
            <Label Content="qwerty2" />
        </gsh:UC.Labels>
    </gsh:UC>
</Grid>

However it still gives me the error at design time:

但是它在设计时仍然给我错误:

Object reference not set to an instance of an object.

你调用的对象是空的。

So if anyone can help me please.

所以如果有人可以帮助我,请。

How can I make UserControl that I can use like native controls which I can populate with collection of elements? I am trying to find the answer second day already.

如何使 UserControl 可以像我可以使用元素集合填充的本机控件一样使用?我已经在第二天试图找到答案。

回答by Richard Friend

I usually check to see if i am in design time before wiring up events etc..

我通常在连接事件等之前检查我是否处于设计时间。

It could be that your Container is null in design mode.

可能是您的 Container 在设计模式下为空。

public class Utils
{
    public static bool IsInDesignerMode
    {
        get
        {
            return ((bool)(DesignerProperties.IsInDesignModeProperty
                .GetMetadata(typeof(DependencyObject)).DefaultValue));
        }
    }

}

Maybe in your constructor you should do this..

也许在你的构造函数中你应该这样做..

public UC()    
{    
    InitializeComponent();    
    if (!Utils.IsInDesignerMode)
    {
        Labels = new ObservableCollection<Label>();    

        Labels.CollectionChanged += new         System.Collections.Specialized.NotifyCollectionChangedEventHandler(Labels_CollectionChanged);    
    }
}  

On another note though i think you would be better using an ItemsControl

另一方面,尽管我认为您最好使用ItemsControl