[英]How to get the text of textbox to textblock in another xaml? c# windows store app
Code in my MainPage.xaml我的 MainPage.xaml 中的代码
<TextBox x:Name="txtBox1" HorizontalAlignment="Left"
Margin="376,350,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Height="14" Width="113"
Text="{Binding TextBox1Text}"/>
Code in my MainPage.xaml.cs我的 MainPage.xaml.cs 中的代码
public string TextBox1Text
{
get { return this.txtBox1.Text; }
set { this.txtBox1.Text = value; }
}
Code in my Page2.xaml我的 Page2.xaml 中的代码
MainPage main = new MainPage();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
txtBlock1.Text = main.TextBox1Text;
}
when i run this there no text that output in my textblock当我运行它时,我的文本块中没有输出文本
A simpler way to do that is to pass parameters between pages:一种更简单的方法是在页面之间传递参数:
MainPage.xaml.cs
: MainPage.xaml.cs
:
private void Button_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Page2), textBox1.Text);
}
And in Page2.xaml.cs
:在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:您可以在List<T>
集合中打包多个对象或创建一个类:
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:在MultiGame.cs
您可以“解包”类中的项目:
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
.您正在创建MainPage
的新实例。 TextBox1Text
isn't initialized with a value. TextBox1Text
未使用值进行初始化。
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如果您希望它成为所有页面共享的值,请创建一个静态类或在 App.cs 文件中声明您的属性
This would be the same as saying.这和说是一样的。
MyCustomClass x = new MyCustomClass();
x.StringProperty = "Im set";
x = new MYCustomClass();
x.StringProperty
isn't set now. x.StringProperty
现在没有设置。
Try this: In MainPage.cs试试这个:在 MainPage.cs
public static MainPage current;
public static Textbox txtbox
Public mainpage()
{
Current = this;
txtbox = this.yourtextboxInXAML;
}
In page2.cs在 page2.cs
public Mainpage.Current.txtbox txtbox;
Now you can use your Mainpage XAML textbox in page2 when ever you want.现在,您可以随时在 page2 中使用 Mainpage XAML 文本框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.