将数据从一个页面发送到另一个页面的通用 windows 应用程序
Universal windows app to send data from one page to another
我将 MainPage.xaml
分为两部分。左侧部分有一些按钮,这些按钮将发出命令以使用新页面更改右侧部分。所以我为 ex 创建了三个正确的页面。 RightPage1
、RightPage2
和 RightPage3
。问题是,我想在 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>
我想我们可以直接在后台代码中完成。例如:
在HomePage.xaml的代码隐藏中,我们可以在HomePage
中定义一个表示HomePage
本身的静态字段,并添加一个public方法来改变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;
}
}
然后在Submit Click事件中,我们可以调用ChangeMessage
方法显示消息。
private void btnSumbit_Click(object sender, RoutedEventArgs e)
{
HomePage.Home?.ChangeMessage("The message you want to show");
}
我将 MainPage.xaml
分为两部分。左侧部分有一些按钮,这些按钮将发出命令以使用新页面更改右侧部分。所以我为 ex 创建了三个正确的页面。 RightPage1
、RightPage2
和 RightPage3
。问题是,我想在 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>
我想我们可以直接在后台代码中完成。例如:
在HomePage.xaml的代码隐藏中,我们可以在HomePage
中定义一个表示HomePage
本身的静态字段,并添加一个public方法来改变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;
}
}
然后在Submit Click事件中,我们可以调用ChangeMessage
方法显示消息。
private void btnSumbit_Click(object sender, RoutedEventArgs e)
{
HomePage.Home?.ChangeMessage("The message you want to show");
}