Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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# 将usercontrol添加到windows窗体c时出错_C#_Winforms_User Controls - Fatal编程技术网

C# 将usercontrol添加到windows窗体c时出错

C# 将usercontrol添加到windows窗体c时出错,c#,winforms,user-controls,C#,Winforms,User Controls,我创建了一个FaceDetectionEvent,它是一个用户控件,并尝试将它添加到windows窗体中,但仍在同一个项目中。但它不断显示出这个错误: 这是FaceDetectionEvent代码: public partial class FaceDetectionEvent : UserControl { private System.Timers.Timer tListener; private MySQL_DataAccess da = new

我创建了一个FaceDetectionEvent,它是一个用户控件,并尝试将它添加到windows窗体中,但仍在同一个项目中。但它不断显示出这个错误:

这是FaceDetectionEvent代码:

 public partial class FaceDetectionEvent : UserControl
    {
        private System.Timers.Timer tListener;
        private MySQL_DataAccess da = new MySQL_DataAccess();
        private int iCurrentStatusIndex_ = 0;
        private List<DataRow> lstFaceDetectionEvent = new List<DataRow>(20);
        private ImageList cropImageList = new ImageList();


        public FaceDetectionEvent()
        {
            InitializeComponent();
            CreateColumns();
            GetLastTwentyEvent();

            tListener = new System.Timers.Timer(1000);
            tListener.Elapsed += new System.Timers.ElapsedEventHandler(tListener_Elapsed);
            tListener.Start();
        }

        public void GetLastTwentyEvent()
        {
            string strSQL = string.Format("SELECT * FROM av_status_log AS A LEFT JOIN avmediaserver AS B ON A.device_id=B.DeviceId "
                                        + "LEFT JOIN privilege_device AS C ON A.device_id = C.device_id "
                                        + "LEFT JOIN privilege_device_group AS D ON C.device_group_id = D.device_group_id "
                                        + "WHERE event_type_id = 8 ORDER BY A.db_time DESC LIMIT 20");
            DataTable dt = da.GetInfoData(strSQL).Tables[0];
            if (dt.Rows.Count > 0)
                iCurrentStatusIndex_ = Convert.ToInt32(dt.Rows[0]["rowid"]);

            foreach (DataRow dr in dt.Rows)
            {
                lstFaceDetectionEvent.Add(dr);
                string strCroppedImage = GetCropImageBase64String(dr["memo"].ToString());
                cropImageList.Images.Add(Base64ToImage(strCroppedImage));
            }

            ShowFDEvent();
        }

        void tListener_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string strSQL = string.Format("SELECT * FROM av_status_log AS A LEFT JOIN avmediaserver AS B ON A.device_id=B.DeviceId "
                                        + "LEFT JOIN privilege_device AS C ON A.device_id = C.device_id "
                                        + "LEFT JOIN privilege_device_group AS D ON C.device_group_id = D.device_group_id "
                                        + "WHERE A.rowid > {0} AND event_type_id = 8 ORDER BY A.db_time DESC", iCurrentStatusIndex_.ToString());
            DataTable dt = da.GetInfoData(strSQL).Tables[0];

            if (dt.Rows.Count > 0)
                iCurrentStatusIndex_ = Convert.ToInt32(dt.Rows[0]["rowid"]);

            if (lstFaceDetectionEvent.Count >= 20)
            {
                lstFaceDetectionEvent.RemoveRange(0, dt.Rows.Count);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cropImageList.Images.RemoveAt(i);
                }
            }

            foreach (DataRow dr in dt.Rows)
            {
                lstFaceDetectionEvent.Add(dr);
                string strCroppedImage = GetCropImageBase64String(dr["memo"].ToString());
                cropImageList.Images.Add(Base64ToImage(strCroppedImage));
            }

            ShowFDEvent();
            this.Refresh();
        }

        public string GetCropImageBase64String(string pStrMemo)
        {
            XElement doc = XElement.Parse(pStrMemo);
            string strCropImage = doc.Element("cropImage").Value;
            return strCropImage;
        }

        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }

        private void CreateColumns()
        {
            ColumnHeader cropImageHeader = new ColumnHeader();
            cropImageHeader.Text = "Crop Image";
            cropImageHeader.Width = 150;
            FDEventlistView.Columns.Add(cropImageHeader);

            ColumnHeader timestampHeader = new ColumnHeader("Event Time");
            timestampHeader.Text = "Event Time";
            timestampHeader.Width = 150;
            FDEventlistView.Columns.Add(timestampHeader);

            ColumnHeader deviceNameHeader = new ColumnHeader("Device Name");
            deviceNameHeader.Text = "Size";
            deviceNameHeader.Width = 80;
            FDEventlistView.Columns.Add(deviceNameHeader);
        }

        private void ShowFDEvent()
        {
            FDEventlistView.Items.Clear();
            FDEventlistView.BeginUpdate();
            int i = 0;
            foreach (DataRow dr in lstFaceDetectionEvent)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = i;

                ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
                subitem.Text = dr["status_time"].ToString();
                item.SubItems.Add(subitem);

                subitem = new ListViewItem.ListViewSubItem();
                subitem.Text = dr["device_name"].ToString();
                item.SubItems.Add(subitem);

                FDEventlistView.Items.Add(item);

                i++;
            }

            FDEventlistView.EndUpdate();
        }
    }

你知道为什么吗?

我认为问题不在于用户控制。为了证明这一点,创建一个新的用户控件,这次不是以编程方式->添加新控件,然后选择UserControl。暂时从MainForm中删除FaceDetectionEvent控件,然后添加新创建的UserControl,然后查看是否再次显示错误。如果有,请共享StackTrace的内容。

用户控件中的代码也将在设计时运行。当使用设计器将控件放到窗体上时,提供控件所见即所得行为的功能。但是肯定会很麻烦,在这种情况下,您不想查询数据库,在Visual Studio中加载控件而不是在程序中加载控件时,您不可能找到正确的连接字符串。仅使用DesignMode属性跳过:

    public FaceDetectionEvent()
    {
        InitializeComponent();
        tListener = new System.Timers.Timer(1000);
        tListener.Elapsed += new System.Timers.ElapsedEventHandler(tListener_Elapsed);
        if (!this.DesignMode) {
            CreateColumns();
            GetLastTwentyEvent();
            tListener.Start();
        }
    }
您可能需要在代码中的其他位置插入DesignMode测试,如绘制和加载事件处理程序


请注意,调试此类仅设计时异常是多么困难,消息框不够大,无法显示堆栈跟踪。在非常困难的情况下,您可能需要调试VisualStudio本身,以便看到异常。启动VS的另一个实例,并使用Tools+Attach to Process将调试器附加到第一个实例。“调试+异常”,勾选“抛出”复选框以在抛出异常时自动断开。

我在尝试将用户控件从工具箱拖动到窗体时也遇到过同样的问题。这似乎很明显,但我的问题涉及到在构造函数中设置参数,如果控件是以编程方式添加的,那么这些参数将在运行时传递

因此,此代码将导致错误。为了避免它,构造函数中没有参数

public ucMyControl(string title)
{
    InitializeComponent();
    groupBox1.Text = title;
}

您在哪一行获得此异常?你调试了你的代码吗?我想这可能是因为两件事。。1.如果您的代码使用多个配置文件,并且在运行时无法合并这些配置文件。2.或者代码在运行时找不到任何特定的dll。出于这个原因,我建议将当前程序需要的所有dll文件复制到bin文件夹中,然后尝试运行并查看..如果我以编程方式添加usercontrol,它可能会工作。我已经将所有dll文件复制到bin文件夹