gpt4 book ai didi

c# - 如何将文本框的文本获取到另一个 xaml 中的文本 block ?时间:2019-03-08 标签:c#windowsstoreapp

转载 作者:行者123 更新时间:2023-11-30 22:10:20 25 4
gpt4 key购买 nike

我的 MainPage.xaml 中的代码

<TextBox x:Name="txtBox1" HorizontalAlignment="Left" 
Margin="376,350,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Height="14" Width="113"
Text="{Binding TextBox1Text}"/>

我的 MainPage.xaml.cs 中的代码

public string TextBox1Text
{
get { return this.txtBox1.Text; }
set { this.txtBox1.Text = value; }
}

我的 Page2.xaml 中的代码

MainPage main = new MainPage();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
txtBlock1.Text = main.TextBox1Text;
}

当我运行它时,我的文本 block 中没有输出文本

最佳答案

一个更简单的方法是在页面之间传递参数:

MainPage.xaml.cs :

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

并且在 Page2.xaml.cs :

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

编辑:看来您想传递多个参数。您可以将多个对象打包在一个 List<T> 中收集或创建一个类:

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

在您的当前页面中:

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

MultiGame.cs您可以“解压”类中的项目:

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

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

关于c# - 如何将文本框的文本获取到另一个 xaml 中的文本 block ?时间:2019-03-08 标签:c#windowsstoreapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com