简体   繁体   English

将方法传递给表单以供以后调用

[英]Passing methods to a form for later invokement

So I have a confusion about how to pass a method with a specific signature to a form that then can invoke said method with its own parameters and evaluate the return value. 因此,我对如何将具有特定签名的方法传递给表单感到困惑,该表单随后可以使用其自己的参数调用该方法并评估返回值。 The problem, the more I read about delegates, events, event-handlers, subscribing and Func and Actions .. the more I am confused. 问题越多,我对委托,事件,事件处理程序,订阅和Func and Actions的了解就越多。 (I have tried lots of them, modified lots of them and non worked, but I suppose thats because I don't get how they work) Example of what I want to do: (我尝试了很多,修改了很多,但没有用,但是我想那是因为我不了解它们的工作原理)我想做的事的例子:

public class WorkingStatic {

    public static SetUpForm() {
        SomeForm tmp_Form = new SomeForm(StaticMethod);
        /*somehow pass the method to the form so that it can invoke it*/
        tmp_Form.Show();
    }

    public static int StaticMethod(int p_Int) {
        // do whatever..
        return p_Int;
    }

}

That is just a class with a method that does something, important is the method takes an int as parameter and returns an int. 那只是一个带有方法执行某些操作的类,重要的是该方法将int作为参数并返回int。

Now comes the Form as I would like it to work .. so code is not working: 现在出现了表单,因为我希望它可以工作..所以代码不起作用:

public partial class SomeForm : Form {

    private Method m_Method;

    public SomeForm(/*here I pass a method*/Method p_Method) {
        InitializeComponent();
        m_Method = p_Method;
    }

    public void SomeMethodThatGetsCalledByAButton() {
        m_Method.Invoke(/*params*/ 1); /*would return 1*/
    }

}

None of this works 'what surprise', and because I am getting kind of frustrated about it I thought I'd ask you guys. 这些都不是什么“惊喜”,因为我对此感到沮丧,所以我想问问你们。

Thanks in advance! 提前致谢!

-RmOL -RmOL

Since the answer I marked got deleted I'll post what worked for me. 自从我标记的答案被删除以来,我将发布对我有用的内容。 Thanks to @Fabio for supplying the solution. 感谢@Fabio提供解决方案。 (as a comment) (作为评论)


Func</*input types here with ',' in between*/, /*output type here*/>

can be handled like just any other type. 可以像其他任何类型一样处理。 (When passing the method do not put normal brackets or any other arguments concerning that method after it) (在传递方法时,请勿在其后加上普通括号或与该方法有关的任何其他参数)

Example like shown in the question would then look like this: 问题中显示的示例如下所示:

public class WorkingStatic {

    public static SetUpForm() {
        SomeForm tmp_Form = new SomeForm(Func<int, int>(StaticMethod));
        /*pass the method to the form so that it can invoke it*/
        tmp_Form.Show();
    }

    public static int StaticMethod(int p_Int) {
        // do whatever..
        return p_Int;
    }

}

public partial class SomeForm : Form {

    private Func<int, int> m_Method;

    public SomeForm(Func<int, int> p_Method) {
        InitializeComponent();
        m_Method = p_Method;
    }

    public void SomeMethodThatGetsCalledByAButton() {
        m_Method(/*params*/ 1); /*would return 1*/
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM