更多WPF窗口之间的对象共享

时间:2013-11-29 11:20:30

标签: c# wpf

我对WPF很新,我需要你帮助一个在更多WPF窗口之间传递的对象。

首先,我的MainWindow使用Button_Click事件,如下所示:

  private void Button_Click(object sender, RoutedEventArgs e)
        {
            Attribute ChooseYourAttr = new Attribute();
            Application.Current.MainWindow.Close();
            ChooseYourAttr.Show();
            Character Player = new Character(firstTextbox.Text);
        }

然后我的第二个窗口名为Attribute,其中包含以下内容:

 private void attributeTopLabel_Initialized(object sender, EventArgs e)
        {
            String welcomeAttribute = "Ahh. I see! So ";
            attributeTopLabel.Content = welcomeAttribute;
        }

我想要这样的东西:(Player.getName());

 private void attributeTopLabel_Initialized(object sender, EventArgs e)
        {
            String welcomeAttribute = "Ahh. I see! So " + Player.getName();
            attributeTopLabel.Content = welcomeAttribute;
        }

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

只需在构造函数中传递值:

private Character player = new Character();

public Attribute(Character player)
{
    this.player = player;
}

...

Character player = new Character(firstTextbox.Text);
Attribute ChooseYourAttr = new Attribute(player);

...

private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
    String welcomeAttribute = "Ahh. I see! So " + player.GetName();
    attributeTopLabel.Content = welcomeAttribute;
}