【问题标题】:Universal windows app to send data from one page to another通用 Windows 应用程序将数据从一个页面发送到另一个页面
【发布时间】:2017-01-01 01:13:43
【问题描述】:

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

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

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

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>

【问题讨论】:

    标签: c# xaml uwp uwp-xaml


    【解决方案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");
    }
    

    【讨论】:

    • 谢谢Jay,我可以这样做,但我不只是想显示textMessage。我的问题就像我有两个 ViewModel 和一个模型,所以我想显示由任何一个 Viewmodel 完成的两个视图的更改。我搜索了答案,我发现我必须创建一个 DataService 来更新所有使用更新模型的 Viewmodel。但我没有得到任何文章来实现相同的。
    • 我不确定这是否是唯一的方法,如果还有其他方法,请帮助。
    • @AnkitSaini 如果您使用的是 MVVM 模式,答案可能会有所不同。我建议您提出一个新问题,详细说明您的 MVVM 模式,例如您的 ViewModels 和 Model 如何实现以及您想要实现的目标。你可以使用一些假日期。这将使您的问题更容易理解,并且您可以获得更好的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 2013-01-24
    • 2014-05-15
    • 2014-11-08
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多