簡體   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