How to get the text of textbox to textblock in another xaml? c# windows store app

﹥>﹥吖頭↗ 提交于 2019-12-06 12:34:17

A simpler way to do that is to pass parameters between pages:

MainPage.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Frame.Navigate(typeof(Page2), textBox1.Text);
}

And in Page2.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    textBlock1.Text = e.Parameter.ToString();
}

Edit: It appears that you want to pass multiple parameters. You can package multiple objects in a List<T> collection or create a class:

public class NavigationPackage
{
    public string TextToPass { get; set; }
    public ImageSource ImgSource { get; set; }
}

In your current page:

private void Button_Click(object sender, RoutedEventArgs e)
{
    NavigationPackage np = new NavigationPackage();
    np.TextToPass = textBox1.Text;
    np.ImgSource = bg2.Source;

    Frame.Navigate(typeof(MultiGame), np);
 }

In MultiGame.cs you can "unpack" the items from the class:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    NavigationPackage np = (NavigationPackage)e.Parameter;

    newTextBlock.Text = np.TextToPass;
    newImage.Source = np.ImgSource;
}

You are creating a new instance of MainPage. TextBox1Text isn't initialized with a value.

If you want it to be a value shared across all of your pages either create a static class or declare your property in the App.cs file

This would be the same as saying.

MyCustomClass x = new MyCustomClass();
x.StringProperty = "Im set";

x = new MYCustomClass();

x.StringProperty isn't set now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!