将变量从窗口传递到页面

问题描述 投票:0回答:1

我想将我插入Window的文本框中的变量传递给WPF应用程序中的页面,我只发现我该怎么做。

基本上,我需要应用程序来提示我需要在其他页面中使用的密码。

我从这样的页面调用窗口:

Password_Prompt PassWindow = new Password_Prompt();
        PassWindow.Show();

它只是带有文本框和按钮的窗口,在我输入密码并单击确定后,我想将密码从我称为窗口的页面发送到变量。

感谢您的回答,

最诚挚的问候,

约翰

c# wpf variables window
1个回答
0
投票

最有效的方法是在单击窗口按钮时创建一个事件,然后从页面中订阅它。

Window

public event EventHandler<string> PasswordInput;

private void NotifyPasswordInput(string password)
{
    PasswordInput?.Invoke(this, password);
}

// button click event handler
private void OnButtonClick(object sender, RoutedEventArgs e)
{
    //get the password from the TextBox
    string password = myTextBox.Text;
    NotifyPasswordInput(passowrd);
}

[Page

...
Password_Prompt PassWindow = new Password_Prompt();

//add this part to subscribe to the event
PassWindow.PasswordInput += OnPasswordInput;

PassWindow.Show();
...

//and the method to handle the event
private void OnPasswordInput(object sender, string password)
{
    //use the password from here
}
© www.soinside.com 2019 - 2024. All rights reserved.