WPF登录后,将用户名存储到另一个xaml

WPF After login,store username to another xaml

我有 2 xaml window,我想在 windows.How 之间存储数据,我可以吗?

使用构造函数传递参考下面的代码示例。

<Window x:Class="DelegateComd_Learning.Login"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Login" Height="300" Width="300">
<Grid>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Username"/>
            <TextBox x:Name="txtUsername" Width="100"/>
        </StackPanel>
        <Button Content="Login" Click="Button_Click"/>
    </StackPanel>
</Grid>

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Window1 win = new Window1(txtUsername.Text);
        win.Show();
    }
}
<Window x:Class="DelegateComd_Learning.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
<Grid>
    <TextBlock x:Name="txtDisplayUsername"/>
</Grid>

 public partial class Window1 : Window
{
    public Window1(string username)
    {
        InitializeComponent();
        txtDisplayUsername.Text = username;
    }
}