【问题标题】:assigning delegate subscribe event to a List将委托订阅事件分配给列表
【发布时间】:2017-01-31 13:33:42
【问题描述】:

这是我的问题:我有一个从另一个班级订阅的代表,没关系。我想要的是每次订阅此委托时都会引发一个事件,告诉我调用列表已更改以及 +1 或 -1... 我在调用列表中搜索了一个 Onchange 事件,但没有找到任何东西..

表格1:

namespace EventsOnDelegates
{
public delegate void DEL_delegate1(Double AValue);
public delegate void DEL_delegate2(Boolean AsecondValue);

public partial class Form1 : Form
{
    public DEL_delegate1 SetValueCbk;
    public EventHandler InvocationListChange;
    private Form2 FormwithLabel;
    int invoclength;
    public Form1()
    {
        InitializeComponent();
        FormwithLabel = new Form2(this);
        FormwithLabel.Show();  
        /*the question part*/
        /*I'd like to add an onchange event that tells me if the invocation list has changed and how + or -*/
        InvocationListChange += new EventHandler(SetValueCbk.GetInvocationList(),InvocationListHaschanged);          
    }
    protected virtual void InvocationListHaschanged(object sender, EventArgs e)
    {
    invoclength = SetValueCbk.GetInvocationList().Length;
    label1.Text = Convert.ToString(invoclength);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Random newRandNum = new Random();
        Double newNumber = newRandNum.NextDouble();
        SetValueCbk(newNumber);
    }
}
}

表格2:

public partial class Form2 : Form
{
    public Form2(){}
    public Form2(Form1 Form1link)
        :this()
    {
        InitializeComponent();
        Form1link.SetValueCbk  += new DEL_delegate1(this.SetValueCbkFN);
    }
    protected void SetValueCbkFN(Double value)
    {
        label1.Text = Convert.ToString(value);
    }
}

感谢您的帮助!

【问题讨论】:

    标签: c# events delegates onchange


    【解决方案1】:

    您可以将explicit event declaration 用于该event 字段:

    private EventHandler meEvent;
    
    public event EventHandler MeEvent
    {
        add { meEvent += value; MeEventInvocationListChanged(); }
        remove { meEvent -= value; MeEventInvocationListChanged(); }
    }
    

    编辑: (适合您的问题)

    您可以创建而不是 InvocationListHasChanged 方法:

    void InvokationListChanged(int dir)
    {
        string msg = dir < 0 ? "Someone unsubscribed from the event" : "Someone subscribed to the event";
        if(InvokeRequired)
        {
            Invoke( new MethodInvoker( () => { label1.Text = msg; });
        }
        else
        {
            label1.Text = msg;
        }
    }
    

    然后将public DEL_delegate1 SetValueCbk;改为:

    private DEL_delegate1 m_SetValueCbk;
    
    public event Del_delegate1 SetValueCbk
    {
        add { m_SetValueCbk+= value; InvokationListChanged(1); }
        remove { m_SetValueCbk-= value; InvokationListChanged(-1); }
    }
    

    现在,每当其他对象订阅SetValueCbk,您的label1.Text 将更改为"Someone subscribed to the event",并且每当某个对象取消订阅SetValueCbk,您的label1.Text 将更改为"Someone unsubscribed from the event"

    【讨论】:

    • 不明白... MeEventInvocationListChanged() 是什么?这个委托的调用列表的链接在哪里?顺便谢谢。
    • 这不是一个属性,它是一个事件,它只是一个带有显式而不是隐式添加/删除处理程序的事件。
    • @Servy 是的,谢谢你的提示。对其进行了编辑并添加了指向 msdn 的链接 :)
    猜你喜欢
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    相关资源
    最近更新 更多