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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21607925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:44:38  来源:igfitidea点击:

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

c#wpfvariables

提问by barack o mama

I have a main window called form1. in form1 I have a button, when it is pressed it will open form2 (form2.ShowDialog()). In form2 I have a button called "Check". When the user clicks on "Check" it should do some validation and if successful creates a string object and return it to form1. Any Ideas on how to implement this? I don't want to return anything when the user closes the window.

我有一个名为 form1 的主窗口。在form1中我有一个按钮,当它被按下时它会打开form2(form2.ShowDialog())。在 form2 中,我有一个名为“检查”的按钮。当用户单击“检查”时,它应该进行一些验证,如果成功则创建一个字符串对象并将其返回到 form1。关于如何实现这一点的任何想法?当用户关闭窗口时,我不想返回任何内容。

回答by Servy

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();
    }
}

回答by j riv

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";

回答by Joe Brunscheon

This could be accomplished in several ways. The method Servy posted is quite good and will accomplish what you need. I would prefer to see the Action<sting>passed as a parameter to the constructor and named callbackso it's clear what it is used for, but that's just a preference thing.

这可以通过多种方式实现。Servy 发布的方法非常好,可以满足您的需求。我更愿意看到Action<sting>作为参数传递给构造函数并命名的,callback所以很清楚它的用途,但这只是一个偏好。

The other method that is pretty good at getting this done is via Messaging. The MVVMLight Toolkitprovides a great little feature in it for accomplishing tasks such as this.

另一种非常擅长完成这项工作的方法是通过消息传递。该MVVMLight工具包在它提供了一个很好的小功能用于完成任务,如这一点。

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
    }
}