C# 从textbox1获取文本,并以其他形式将其设置在textblock1上

C# 从textbox1获取文本,并以其他形式将其设置在textblock1上,c#,windows-8,windows-runtime,C#,Windows 8,Windows Runtime,我想从form1上的textbox1获取输入,并将其设置在form2的textblock1上 但是文本不会显示 表格1代码: private void Button_Click_3(object sender, RoutedEventArgs e) { var file = await ApplicationData.Current.LocalFolder.GetFileAsync(textbox1.Text + ".txt"); var line

我想从form1上的textbox1获取输入,并将其设置在form2的textblock1上 但是文本不会显示

表格1代码:

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        var file = await ApplicationData.Current.LocalFolder.GetFileAsync(textbox1.Text + ".txt");
        var line = await FileIO.ReadLinesAsync(file);
        if (textbox1.Text == line[0] && tb2.Password == line[1])
           {
               textbox1.Text = line[0];
               Frame.Navigate(typeof(form2));                    
           }
    }

    public string MyValue
    {
        get { return textbox1.Text; }
    }
表格2上的代码

    var logged = new MainPage();
    textblock1.Text = logged.MyValue;
有什么帮助吗?
谢谢:D

您在
表单2中没有得到所需的值,因为您正在创建
主页
的新实例,该实例当然不包含您刚刚导航的实例的值

您需要将该值作为参数传递到
form2
。为此,您可以使用另一个重载
导航
,该重载包含一个附加参数:

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync(textbox1.Text + ".txt");
    var line = await FileIO.ReadLinesAsync(file);
    if (textbox1.Text == line[0] && tb2.Password == line[1])
    {
        textbox1.Text = line[0];
        Frame.Navigate(typeof(form2), textbox1.Text);                    
    }
}

您在
form2
中无法获得所需的值,因为您正在创建
MainPage
的新实例,该实例当然不包含您刚刚从中导航的实例的值

您需要将该值作为参数传递到
form2
。为此,您可以使用另一个重载
导航
,该重载包含一个附加参数:

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync(textbox1.Text + ".txt");
    var line = await FileIO.ReadLinesAsync(file);
    if (textbox1.Text == line[0] && tb2.Password == line[1])
    {
        textbox1.Text = line[0];
        Frame.Navigate(typeof(form2), textbox1.Text);                    
    }
}
试一试 表格1

表格2

这可能会有帮助

试试看 表格1

表格2

这也许有帮助

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string name = e.Parameter as string;

    if (!string.IsNullOrWhiteSpace(name))
    {
          textblock1.Text = name ;
    }
    else
    {
        textblock1.Text = "Name is required.  Go back and enter a name.";
    }
}