在C#,UWP的不同页面上将字符串写入textblock [英] write string to textblock on different page in C#, UWP

查看:66
本文介绍了在C#,UWP的不同页面上将字符串写入textblock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在另一个页面上写入 TextBlock ?
到目前为止,它仅适用于同一页面上的 TextBlocks .
async 函数位于 Page_1 中. TextBlock 位于 Page_2 上.

How can I write into a TextBlock on a different page?
So far it only works with TextBlocks on the same page.
The async function is in Page_1. The TextBlock is on Page_2.

public async void Serial() 
{
   string rxBuffer;
   //code
   //code
   //code

   while (true)
   {
      textblock_DebugRx_Gas_live.Text = rxBuffer;    
   } 
} 

推荐答案

在C#,UWP的不同页面上将字符串写入文本块

write string to textblock on different page in C#, UWP

如果两个页面同时显示在前台,如下所示.

If the two page display in foreground at same time like following.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <StackPanel Margin="0,20,0,0" HorizontalAlignment="Center">
        <Button Click="Button_Click" Content="Click" />
        <TextBlock x:Name="Tbk" />
    </StackPanel>

    <Frame Grid.Row="1" VerticalAlignment="Center">
        <local:TestPage />
    </Frame>
</Grid>

您可以使用 Messenger 将消息从原始页面发送到目标页面./p>

You could use Messenger to send message from original page to target page.

using GalaSoft.MvvmLight.Messaging;

private void Button_Click(object sender, RoutedEventArgs e)
{
    var message = "Test";
    Messenger.Default.Send<string,TestPage>(message);
    Tbk.Text = message;
}

目标页面

public TestPage()
{
    this.InitializeComponent();
    this.Loaded += TestPage_Loaded;
}

private void TestPage_Loaded(object sender, RoutedEventArgs e)
{
    Messenger.Default.Register<string>(this, (s) =>
    {
        MyTextBlock.Text = s;
    });
} 

这篇关于在C#,UWP的不同页面上将字符串写入textblock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆