Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Visual Studio在尝试添加我的控件时出现NullReferenceException!_C#_User Controls - Fatal编程技术网

C# Visual Studio在尝试添加我的控件时出现NullReferenceException!

C# Visual Studio在尝试添加我的控件时出现NullReferenceException!,c#,user-controls,C#,User Controls,以下是错误: 它说异常的来源在我的类的第20行。这是我的班级: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections.ObjectMo

以下是错误:

它说异常的来源在我的类的第20行。这是我的班级:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace WebServiceScanner
{
    public partial class imageList : UserControl
    {
        private int XPosition = 0;

        public imageList()
        {
            InitializeComponent();
            Images.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Images_CollectionChanged);
        }       

        public ObservableCollection<selectablePicture> Images { get; set; }

        public void AddImage(selectablePicture image)
        {
            Images.Add(image);
        }

        public void RemoveImage(selectablePicture image)
        {
            Images.Remove(image);
        }

        public void MoveImageLeft(int index)
        {
            selectablePicture tmpImage = Images[index];
            Images[index] = Images[index - 1];
            Images[index - 1] = tmpImage;
        }

        public void MoveImageRight(int index)
        {
            selectablePicture tmpImage = Images[index];
            Images[index] = Images[index + 1];
            Images[index + 1] = tmpImage;
        }

        void Images_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RedrawImages();
        }

        private void RedrawImages()
        {
            foreach (var picture in Images)
            {
                picture.Location = new Point(XPosition + panel1.AutoScrollPosition.X, 0);
                XPosition += 130;
                panel1.Controls.Add(picture);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用系统数据;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Collections.ObjectModel;
命名空间WebServiceScanner
{
公共部分类imageList:UserControl
{
私有int XPosition=0;
公共图像列表()
{
初始化组件();
Images.CollectionChanged+=新系统.Collections.Specialized.NotifyCollectionChangedEventHandler(Images\u CollectionChanged);
}       
公共可见收集映像{get;set;}
公共无效添加图像(可选择图片图像)
{
图片。添加(图片);
}
公共无效删除图像(可选择图片图像)
{
图像。删除(图像);
}
公共void MoveImageLeft(整型索引)
{
selectablePicture tmpImage=图像[索引];
图像[索引]=图像[索引-1];
图像[索引-1]=TMP图像;
}
公共void MoveImageRight(整数索引)
{
selectablePicture tmpImage=图像[索引];
图像[索引]=图像[索引+1];
图像[索引+1]=TMP图像;
}
无效图像\u CollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
重画图像();
}
私有void重绘图像()
{
foreach(图像中的var图片)
{
picture.Location=新点(XPosition+panel1.AutoScrollPosition.X,0);
XPosition+=130;
面板1.控件。添加(图片);
}
}
}
}
也许我在做一些愚蠢的事情,比如在构造函数中设置事件处理程序。有什么想法吗?这个用户控件实际上并没有那么复杂,所以很少有地方会出错


如果你们需要更多信息,请告诉我。

添加一条语句来实例化
图像

public imageList()
{
    InitializeComponent();

    Images = new ObservableCollection<selectablePicture>();
    Images.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Images_CollectionChanged);
}
公共图像列表()
{
初始化组件();
图像=新的ObservableCollection();
Images.CollectionChanged+=新系统.Collections.Specialized.NotifyCollectionChangedEventHandler(Images\u CollectionChanged);
}

我猜这与图像未设置为任何值有关,因此控件尝试为DesignView呈现图像,但图像为空,因此出现NullReferenceException。它可能是构造函数中的代码,当它尝试针对可能在任何注入代码设置图像之前运行的图像注册时。

是否
具有无参数公共构造函数?你是在实例化
图像吗?

将其添加到我的构造函数中,现在给了我一个错误:我被难住了。这可能是什么原因造成的。此消息表示需要将
selectablePicture
类标记为
Serializable
,以便可以序列化该对象以进行webservice通信。另外,感谢您发布截图。这些都很有用@菲尔:为什么这需要出现在画面中?关于如何解决这个问题有什么建议吗?@Serg-当您需要通过Web服务公开类型时,需要序列化对象。webservice调用中只能公开涉及可以序列化(转换为某些常见约定格式)的对象。使用
Serializable
将要求您拥有公共无参数构造函数,除非您实现
ISerializable
,IIRM…不,我没有无参数构造函数。为什么我需要一个呢?我收到一个类型错误,其中我没有无参数的公共构造函数。我真的不确定这是否是例外的原因——只是认为我会提供它作为一种可能性。