调用时不存在Classes事件? [英] Classes event doesn't exist when called?

查看:99
本文介绍了调用时不存在Classes事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Widget,它是一个userControl.窗口小部件包含一个自定义类CustomImageViewer.

CustomImageViewer通过一个非常简单的事件发布消息.窗口小部件订阅此事件,并在引发该事件时引发它自己的事件以传递CustomImageViewer消息以及它自己的一些信息.

这是Widget类中传递消息的代码:

I have a class, Widget, which is a userControl. Widget contains a custom class, CustomImageViewer.

CustomImageViewer publishes messages via a very simple event. Widget subscribes to this event and, when it is raised, throws it''s own event to pass along the CustomImageViewer message, along with some of it''s own info.

Here''s the code from the Widget class which passes along the message:

void civImgView_NoteSent(string theMessage, cvMessaging.messageStatus theStatus)
        {
//we recieved a message from the civImgView_NoteSent.  Just pass it along.
this.NoteSent("Control " + ID.ToString() + ": " + theMessage, theStatus);
            
        }



这些小部件通过后台工作线程添加到流布局面板中.我遇到的问题是,上面的行"this.NoteSent("Control" + ...随机抛出一个错误,指出对象引用未设置为对象的实例".

将以上内容更改为以下内容可防止错误:



The widgets are added to a flow layout panel by a background worker thread. The problem I''m having is that, at random, the above line "this.NoteSent("Control " +..." throws an error stating "object reference not set to an instance of an object".

Changing the above to the following prevents the error:

void civImgView_NoteSent(string theMessage, cvMessaging.messageStatus theStatus)
        {

            //we recieved a message from the civImgView_NoteSent.  Just pass it along.
            if (this.NoteSent != null)
            {
this.NoteSent("Control " + ID.ToString() + ": " + theMessage, theStatus);
            }
            
        }


我这样做的时候显然只有几条消息...

"NoteSent"是在小部件类中定义的事件,如下所示:


I''m obviously only getting a few of the messages when I do that though...

"NoteSent" is an event defined in the widget class like this:

public partial class cvWidget : UserControl
  {



      private delegate void delStringOnly(string theString);
      private delegate void delNoVar();

      //Standard event to be raised when the widget has a message to give to someone who cares.
      public event cvMessaging.delMessageDelegate NoteSent;
      //the id of this widget - defined by the calling class
      public int ID { get; set; }



那我想念什么呢?如果引发事件的位置在引发错误的类中,则如何不存在事件"NoteSent"?那有意义吗?如果未实例化Widget类,那么如何运行"civImgView_NoteSent"子级;如果是Widget类,则该事件如何不能运行?

不好意思,如果说的不好,但是希望您能得到我所问的.这是课程第一部分的代码:



So what am I missing? How can the event "NoteSent" not exist, if the place where it is raised is WITHIN the class where the error is raised? Does that make sense? How can the Widget class run the "civImgView_NoteSent" sub if it is not instantiated, and if the Widget is, how can the event not be?

Sorry if that''s poorly phrased, but hopefully you get what I''m asking. Here''s the code for the first part of the class:

 public partial class cvWidget : UserControl
    {

        

        private delegate void delStringOnly(string theString);
        private delegate void delNoVar();

        //Standard event to be raised when the widget has a message to give to someone who cares.
        public event cvMessaging.delMessageDelegate NoteSent;
        //the id of this widget - defined by the calling class
        public int ID { get; set; }
        private CustomImageViewer civImgView;

        //if the contained CustomImageViewer is selected then this should be passed
        //on as a property of this control as well
        public bool isSelected
        {
            get
            {
                if (this.civImgView.selectedState == CustomImageViewer.selectedStates.Selected)
                { return true; }
                else { return false; }


            }
        }
        //The path to the file this widget represents
        private string _filePath;
        public string filePath
        {
            get
            {
                return _filePath;
            }

            set
            {
                _filePath = value;
                setFileLabel();
                setWidgetImage();
            }
        }
        



        public cvWidget()
        {
            InitializeComponent();

            // 
            // civImgView
            // 
            this.civImgView = new CleanViewLib.CustomImageViewer(this);
            this.Controls.Add(civImgView);
            this.civImgView.BackColor = System.Drawing.Color.Gray;
            this.civImgView.Location = new System.Drawing.Point(3, 3);
            this.civImgView.Name = "civImgView";
            this.civImgView.Padding = new System.Windows.Forms.Padding(1);
            this.civImgView.Size = new System.Drawing.Size(112, 82);
            this.civImgView.TabIndex = 2;
            //set up event handler that handles messages sent from the image viewer.  These
            //get passed on from this controls message event as well
            this.civImgView.NoteSent += new cvMessaging.delMessageDelegate(civImgView_NoteSent);
        }

        void civImgView_NoteSent(string theMessage, cvMessaging.messageStatus theStatus)
        {

            //we recieved a message from the civImgView_NoteSent.  Just pass it along.
            if (this.NoteSent != null)
            {
this.NoteSent("Control " + ID.ToString() + ": " + theMessage, theStatus);
            }
            
        }



我知道问题与以下事实有关:将小部件添加为后台线程的一部分,但是我仍然不知道该事件如何不存在,因为包含它的对象本身确实存在...

谢谢一束.

符文



I know the issue has something to do with the fact that the widgets are added as part of the background thread, but I still don''t get how the event can not exist is the object itself that contains it does...

Thanks a bunch.

Rune

推荐答案

实际上,不存在事件.如果未订阅事件处理程序,则该事件将始终为null.

当没有事件处理程序订阅时,this.NoteSend == null.
当订阅了一个或多个事件处理程序时,this.NoteSend!= null.


因此,将NoteSend包装在if块中始终是一个好主意,这样,如果您只是创建对象而无需订阅事件处理程序,则它可以平稳运行.

你明白我的意思吗?
Practically speaking there is no existence of an Event. The event will always give a null if it is not subscribed to an event handler.

this.NoteSend == null when no eventhandler is subscribed to it.
this.NoteSend != null when one or more eventhandler is subscribed to it.


So it is always a good idea to wrap the NoteSend inside an if block such that if you are just creating the object without subscribing to an event handler it runs smoothly.

You got my point ?


这篇关于调用时不存在Classes事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆