通用Windows应用程序将数据从一个页面发送到另一个页面

时间:2016-07-12 13:19:00

标签: c# xaml uwp uwp-xaml

我将MainPage.xaml分为两部分。左侧部分有一些按钮,可以命令用新页面更改右侧部分。所以我为前面创建了三个正确的页面。 RightPage1RightPage2RightPage3。问题是,我希望在RightPage1,2或3上完成操作后显示左侧部分的数据。 我应该遵循一些模式来实现这种方法吗?或者我们可以直接在代码后面做到这一点?

我所研究的只是为我提供导航到该页面并在参数中发送数据的解决方案。但是我不想再次打开页面,因为它已经在MainPage的左侧打开了。请帮我解决这个问题。

在RightPage 1中,提交点击事件我想在MainPage.xaml lblClassName的TextBlock左侧部分显示一些消息。

HomePage.xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="5*"/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>
    <RelativePanel>
        <Button x:Name="btn1"
                Content="Button 1"
                Height="50" Width="100" Margin="0,30,0,0"
                RelativePanel.AlignHorizontalCenterWithPanel="True"
                Click="btn1_Click"/>
        <Button x:Name="btn2"
                Content="Button 2"
                Height="50" Width="100" Margin="0,30,0,0"
                RelativePanel.AlignHorizontalCenterWithPanel="True"
                RelativePanel.Below="btn1"
                Click="btn2_Click"/>
        <Button x:Name="btn3"
                Content="Button 3"
                Height="50" Width="100" Margin="0,30,0,0"
                RelativePanel.AlignHorizontalCenterWithPanel="True"
                RelativePanel.Below="btn2"
                Click="btn3_Click"/>

        <TextBlock x:Name="lblWrite"
                   Text="Write something : "
                   Visibility="Visible"
                   RelativePanel.Below="btn3"/>
        <TextBox x:Name="txtWrite"
                 Height="50" Width="150"
                 Visibility="Collapsed"
                 RelativePanel.Below="lblWrite"/>
        <Button x:Name="btn3_1"
                Height="50" Width="100"
                Visibility="Collapsed"
                Content="Send"
                RelativePanel.Below="txtWrite"/>

        <TextBlock x:Name="lblClassName"/>

    </RelativePanel>
    <Frame x:Name="RightPage"
           Grid.Column="1"/>
</Grid>


RightPage1.xaml

<Grid Background="Beige">
        <TextBlock x:Name="heading"
                   Text="Teacher Module"
                   RelativePanel.AlignHorizontalCenterWithPanel="True"/>
        <TextBlock x:Name="lblName"
                   Text="Name" Margin="0,30,0,0"
                   RelativePanel.AlignHorizontalCenterWithPanel="True"
                   RelativePanel.Below="heading"/>
        <TextBox x:Name="txtName"
                 Height="30" Width="150" Margin="30,30,0,0"
                 RelativePanel.RightOf="lblName"
                 RelativePanel.Below="heading"/>
        <TextBlock x:Name="lblClass"
                   Text="Class" Margin="0,30,0,0"
                   RelativePanel.AlignHorizontalCenterWithPanel="True"
                   RelativePanel.Below="lblName"/>
        <TextBox x:Name="txtClass"
                 Height="30" Width="150" Margin="30,10,0,0"
                 RelativePanel.RightOf="lblClass"
                 RelativePanel.Below="txtName"/>
        <Button x:Name="btnSumbit"
                Content="Submit"
                Height="50" Width="100" Margin="0,30,0,0"
                RelativePanel.AlignHorizontalCenterWithPanel="True"
                RelativePanel.Below="lblClass"/>
        <Button x:Name="btnCancel"
                Content="Cancel"
                Height="50" Width="100" Margin="30,30,0,0"
                RelativePanel.AlignHorizontalCenterWithPanel="True"
                RelativePanel.Below="lblClass"
                RelativePanel.RightOf="btnSumbit"/>

        <TextBlock x:Name="lblResult"
                   Margin="0,30,0,0"
                   RelativePanel.Below="btnSumbit"/>
    </RelativePanel>
</Grid>

1 个答案:

答案 0 :(得分:1)

我认为我们可以直接在代码中执行此操作。例如:

在HomePage.xaml的代码隐藏中,我们可以在HomePage中定义一个代表HomePage本身的静态字段,并添加一个公共方法来更改TextBlock的文本

public sealed partial class HomePage : Page
{
    //define a static field represent the HomePage itself
    public static HomePage Home;

    public HomePage()
    {
        this.InitializeComponent();
        //initialize Home field
        Home = this;
    }

    ...

    /// <summary>
    /// Show some message in TextBlock lblClassName
    /// </summary>
    /// <param name="message">message to been shown</param>
    public void ChangeMessage(string message)
    {
        this.lblClassName.Text = message;
    }
}

然后在提交点击事件中,我们可以调用ChangeMessage方法来显示消息。

private void btnSumbit_Click(object sender, RoutedEventArgs e)
{
    HomePage.Home?.ChangeMessage("The message you want to show");
}