简体   繁体   English

C#在Windows之间传递数据(WPF)

[英]C# Passing data Between windows(WPF)

Recently picked up C# in university, trying to work out how to pass the variable "name" in MainWindow.xaml to ThirdWindow.xaml? 最近在大学里学习了C#,试图弄清楚如何将MainWindow.xaml中的变量“ name”传递给ThirdWindow.xaml?

The below code is for the main window where the data is assigned to the variable "name" 以下代码用于将数据分配给变量“名称”的主窗口

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();    
    }

    public void NameBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string name = NameBox.Text;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SecondWindow newWin = new SecondWindow();
        newWin.Show();
        this.Close();
    }
}

The below code is for the third window 以下代码用于第三个窗口

public partial class ThirdWindow : Window
{
    public ThirdWindow()
    {
        InitializeComponent();      
    }

    public void LstThanks_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LstThanks.Items.Add(name);
    }
}

You can simply pass that string name variable in the constructor as an argument to the ThirdWindow on Button_Click event. 您只需在构造函数中将该字符串名称变量作为参数传递Button_Click事件上的ThirdWindow

   private void Button_Click(object sender, RoutedEventArgs e)
    {
        var name = "your name";
        var newWin = new ThirdWindow(name);
        newWin.Show();
        this.Close();
    }

That string text will be available in the constructor of ThirdWindow . 该字符串文本将在ThirdWindow的构造函数中提供

public partial class ThirdWindow: Window
{
    public ThirdWindow(string name)
    {
        InitializeComponent();      
    }

    public void LstThanks_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LstThanks.Items.Add(name);
    }
}

You can pass the variable through the constructor of the new window 您可以通过新窗口的构造函数传递变量

var win = new ThirdWindow(name);

public ThirdWindow(string name)
{
    InitializeComponent();      
}

Another method is to pass it through an event message. 另一种方法是通过事件消息传递它。 This will require you to write a new message and add an event listener to your constructor in the ThirdWindow class. 这将要求您编写一条新消息,并将事件侦听器添加到ThirdWindow类中的构造函数。 If you google this there are a variety of examples out there on how to do such a thing. 如果您使用google进行搜索,这里有各种各样的示例。

Here you are defining a local variable name. 在这里,您将定义一个局部变量名称。 This variable is visible only inside the {} block. 该变量仅在{}块内部可见。 So we cannot use it anywhere else. 因此,我们无法在其他任何地方使用它。

public void NameBox_TextChanged(object sender, TextChangedEventArgs e)
{
    string name = NameBox.Text;
}

You could add a new string property into second window and pass value through that to all the way into third form. 您可以在第二个窗口中添加一个新的字符串属性,然后将值一直传递到第三种形式。

So, add new property into your two windows (SecondWindow, ThirdWindow) 因此,将新属性添加到两个窗口(SecondWindow,ThirdWindow)中

public string Name { get; set; }

These properties are holding the data for whole their lifetime (until they are closed). 这些属性将在整个生命周期中保存数据(直到关闭)。

Remove NameBox_TextChanged event handling, because we don't need it. 删除NameBox_TextChanged事件处理,因为我们不需要它。 Add property setting inside your buttons click event 在按钮单击事件中添加属性设置

private void Button_Click(object sender, RoutedEventArgs e)
{
    SecondWindow newWin = new SecondWindow();
    newWin.Name = NameBox.Text; //Store value into SecondWindow variable
    newWin.Show();
    this.Close();
}

Now when SecondWindow is visible (Show is called), you have name value available in Name variable and you should be able to copy this behavior for ThirdWindow. 现在,当SecondWindow可见(调用Show)时,您可以在Name变量中使用name值,并且应该能够为ThirdWindow复制此行为。

If the ThirdWindow window is dependent on the name value then you can pass it through the constructor: 如果ThirdWindow窗口依赖于name值,则可以将其传递给构造函数:

public partial class ThirdWindow : Window
{
    public string Name { get; set; }

    public ThirdWindow(string name)
    {
        InitializeComponent();   
        Name = name;
    }
}

or if not then make a method on the ThridWindow to set the name : 否则,请在ThridWindow上创建一个方法来设置name

public partial class ThirdWindow : Window
{
    public string Name { get; set; }

    public void SetName(string name)
    {  
        Name = name;
    }
}

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

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