繁体   English   中英

从textbox1获取文本,并以其他形式在textblock1上进行设置

[英]get text from textbox1 and set it on textblock1 in other form

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

form1代码:

    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; }
    }

Form2上的代码

    var logged = new MainPage();
    textblock1.Text = logged.MyValue;

有什么帮助吗? 感谢:D

试试form1

 Frame.Navigate(typeof(form2), textbox1.Text);   

窗口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.";
    }
}

链接可能有帮助

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

您将需要将该值作为参数传递给form2 为此,您可以使用不同的Navigate重载,该重载带有一个附加参数:

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);                    
    }
}

暂无
暂无

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

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