简体   繁体   English

C# - 在WPF中将子窗口中的变量返回到父窗口

[英]C# - Return variable from child window to parent window in WPF

I have a main window called form1. 我有一个名为form1的主窗口。 in form1 I have a button, when it is pressed it will open form2 (form2.ShowDialog()). 在form1我有一个按钮,按下它将打开form2(form2.ShowDialog())。 In form2 I have a button called "Check". 在form2中,我有一个名为“Check”的按钮。 When the user clicks on "Check" it should do some validation and if successful creates a string object and return it to form1. 当用户点击“Check”时,它应该进行一些验证,如果成功,则创建一个字符串对象并将其返回到form1。 Any Ideas on how to implement this? 有关如何实现这一点的任何想法? I don't want to return anything when the user closes the window. 当用户关闭窗口时,我不想返回任何内容。

Create an event in your second window, have the parameters of the event's delegate contain whatever information you want to pass: 在第二个窗口中创建一个事件,让事件委托的参数包含您要传递的任何信息:

public class Popup : Window
{
    public event Action<string> Check;

    public void Foo()
    {
        //fire the event
        if (Check != null)
            Check("hello world");
    }
}

Then the main window can subscribe to that event to do what it wants with the information: 然后主窗口可以订阅该事件以使用该信息执行所需操作:

public class Main : Window
{
    private Label label;
    public void Foo()
    {
        Popup popup = new Popup();
        popup.Check += value => label.Content = value;
        popup.ShowDialog();
    }
}

This answer while not perfectly on target will be more useful to people that google themselves here for the general solution of how to communicate between windows: 这个答案虽然不是完美的目标对于那些谷歌自己在这里为如何在Windows之间进行通信的一般解决方案的人更有用:

There is no reason at all to set up events to access objects of the Main Window of you application. 完全没有理由设置事件来访问应用程序主窗口的对象。 You can simply call them on the popup code and be done with it: 你可以简单地在弹出代码上调用它们并完成它:

((MainWindow)Application.Current.MainWindow).textBox1.Text = "Some text";

This could be accomplished in several ways. 这可以通过几种方式实现。 The method Servy posted is quite good and will accomplish what you need. Servy发布的方法非常好,可以满足您的需求。 I would prefer to see the Action<sting> passed as a parameter to the constructor and named callback so it's clear what it is used for, but that's just a preference thing. 我更希望看到Action<sting>作为参数传递给构造函数并命名为callback因此很清楚它的用途是什么,但这只是一个偏好的东西。

The other method that is pretty good at getting this done is via Messaging. 完成此任务的另一种方法是通过Messaging。 The MVVMLight Toolkit provides a great little feature in it for accomplishing tasks such as this. MVVMLight Toolkit为完成此类任务提供了一个很棒的功能。

Step 1: create a strongly typed Message: 第1步:创建强类型消息:

public class MyMessage : MessageBase
{
    //Message payload implementation goes here by declaring properties, etc...
}

Step 2: Determine where, and when to publish that message. 第2步:确定发布该消息的位置和时间。

public class PublishMessage
{
    public PublishMessage(){
    }

    public void MyMethod()
    {
        //do something in this method, then send message:
        Messenger.Default.Send(new MyMessage() { /*initialize payload here */ });
    }
}

Setp 3: Now that you are sending the message, you need to be able to receive that message: 第3步:现在您正在发送消息,您需要能够接收该消息:

public class MessageReceiver
{
    public MessageReceiver()
    {
        Messenger.Default.Register<MyMessage>(this, message => Foo(message));
    }

    public void Foo(MyMessage message)
    {
        //do stuff
    }
}

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

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