【发布时间】:2019-01-06 16:16:23
【问题描述】:
我正在开发一个有两个页面的 Windows 应用程序 (UWP),我想要在页面之间传递参数的最佳实践。
这是我的场景:
我们有两个页面,每个页面都打开并保持在屏幕中间,每个页面上都有一个按钮,当我们点击它时,它会将消息发送到另一个页面。
我也想不断重复地传递信息。
在 Page1.cs 中:
Page2 page2;
public Page1()
{
this.InitializeComponent();
CreatPage2();
}
// creat page 2
private async void CreatPage2()
{
var NewWindow = CoreApplication.CreateNewView();
int NewWindowid = 0;
await NewWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
Frame newframe = new Frame();
newframe.Navigate(typeof(Page2), this);
Window.Current.Content = newframe;
Window.Current.Activate();
ApplicationView.GetForCurrentView().Title = "page2";
NewWindowid = ApplicationView.GetForCurrentView().Id;
});
await Windows.UI.ViewManagement.ApplicationViewSwitcher.TryShowAsStandaloneAsync(NewWindowid);
}
//Button
private void ChangeP2_Click(object sender, RoutedEventArgs e)
{
// send a message to the texblock in the page2
page2.TexBlock2.Text=$"From page1 :{e.ToString()}";
// change text color of the texblock in the page2
page2.Foreground= new SolidColorBrush(Windows.UI.Colors.Red);
}
在 Page2.cs 中:
Page1 page1;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
page1 = e.Parameter as Page1;
base.OnNavigatedTo(e);
}
public Page2()
{
this.InitializeComponent();
}
//Button
private void ChangeP1_Click(object sender, RoutedEventArgs e)
{
// send a message to the texblock in the page1
page1.TexBlock1.Text=$"From page2 :{e.ToString()}";
// change text color of the texblock in the page1
page1.Foreground= new SolidColorBrush(Windows.UI.Colors.Red);
}
上面的代码只适用于 page2 到 page1。 (它可以改变pagea的文本块)。 请帮助我,我找不到适用于两页的解决方案
【问题讨论】:
-
你尝试过简单的事件吗?
-
不,如果你知道请帮助我。