0

我正在编写一个简单的应用程序,当单击按钮时绑定了一个命令,我正在做

 {...
newEvent.ExecuteTargets += exacuteNewEvent;
    }

    void exacuteNewEvent(string message)
    {
        Window1 w = new Window1();
        w.ShowDialog();

    }

我的问题是 w 如何调用我的主窗口以知道它有一个新的 nassage 我应该插入以委托他应该调用的窗口的 wa 方法吗?还有另一种方法吗?

4

1 回答 1

0

传递对当前表单的引用并使用它来调用它的函数。

class Form1 
{
    void Function() 
    {
        newEvent.ExecuteTargets += exacuteNewEvent;
    }

    void exacuteNewEvent(string message)
    {
        Window1 w = new Window1(this);
        w.ShowDialog();
    }

    public void ExecuteStuffInOtherWindow() 
    {
        // do something
    }
}

class Window1 
{
    Form _otherForm;

    public Window1(Form f) 
    {
        _otherForm = f;
        _otherForm.ExecuteStuffInOtherWindow(); // call code in other form
    }
}
于 2010-11-07T09:55:19.497 回答