如何在页面之间传递信息 [英] How to pass information between pages

查看:62
本文介绍了如何在页面之间传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在开发一个UWP应用程序有两页,我希望最佳实践在页面之间传递参数。  I
也希望连续不断地传递信息




以下场景的最佳方法是什么?



例如,更改页面中的用户控件。我们有两个页面, 每个
打开并保持在屏幕中间
 以及每个页面上的一个按钮,它运行一个方法,并在我们点击它时更改另一个页面的背景颜色。



创建页面功能:

 private async void CreatPage2()
{
var NewWindow = CoreApplication.CreateNewView();
int Windowid = ApplicationView.GetForCurrentView()。Id;
int NewWindowid = 0;

await NewWindow.Dispatcher.RunAsync (CoreDispatcherPriority.Normal,()=>
{
Frame newframe = new Frame();
newframe.Navigate(typeof(Page2),this);
Window.Current .Content = newframe;
Window.Current.Activate();
ApplicationView.GetForCurrentView()。Title =" page2" ;;

NewWindowid = ApplicationView.GetForCurrentView()。 Id;
});

等待Windows.UI.ViewManagement.ApplicationViewSwitcher.TryShowAsStandaloneAsync(NewWindowid);

}

A我用过" OnNavigatedTo NavigationEventArgs < span class ="pln"style ="font-family:inherit; font-size:13px; font-style:inherit; font-variant:inherit; font-weight:inherit; white-space:inherit; background-color:# eff0f1; margin:0px; padding:0px; border:0px; line-height:inherit; vertical-align:baseline; color:#303336">
e )"  page2.cs并且它运行良好。但我不能在page1中这样做。




解决方案

看看主题 数据绑定和MVVM 一般的最佳做法是每个视图彼此分开,并且联合数据
稍后将保存在单独的数据中。两个视图将绑定到数据层,以便它们(可选地)对其进行更改并获得数据更改的通知。然后,View可以使用传递的信息更新数据模型,数据对象将
然后发出通知,并且查看两个可以通过更新它显示的内容来响应通知。


LucaLindholm在Stackoverflow上发布了一个很好的回复(包括示例代码)给我的同学  https://stackoverflow.com/questions/54 063454 /最佳实践 - 传递 - 参数 - 打开页面之间


Luca没有做过一次棘手的事情地址是你不只有两个页面:你有两个视图,所以他们每个都运行自己的调度程序线程。这意味着通知处理程序需要确保使用Dispatcher.RunAsync方法将
的任何通知编组到该视图自己的线程。


I'm developing a UWP application that has two pages, I want the best practice to pass parameters between pages. I also want to pass information continuously and repeatedly

What is the best method for the following scenario?

For example, change user controls in the pages.We have two pages,each open and remain at the middle of the screen and a button on each page, which runs a method and changes the background color of the other page when we click on it.

create page function:

 private async void CreatPage2()
        {
            var NewWindow = CoreApplication.CreateNewView();
            int Windowid = ApplicationView.GetForCurrentView().Id;
            int NewWindowid = 0;

            await NewWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                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);

        }

Also I used "OnNavigatedTo(NavigationEventArgs e)" in the page2.cs and it's work well. But i can't do that in the page1.


解决方案

Take a look at the topic Data binding and MVVM. The general best practice is for each view to be separate from each other, and for joint data to be kept in a separate data later. Both views will bind to the data layer so they can (optionally) make changes to it and get notifications of changes to the data. View one can then update the data model with the passed information, the data object will then raise a notification, and view two can respond to the notification by updating what it shows.

LucaLindholm posted a good response (including sample code) to what I assume is your classmate on Stackoverflow at https://stackoverflow.com/questions/54063454/best-practice-to-pass-parameters-between-open-pages

One trickiness that you'll have that Luca didn't address is that you don't just have two Pages: you have two Views, so they'll each run their own dispatcher threads. This means that the notification handlers will need to make sure to marshal any notifications to that view's own thread with the Dispatcher.RunAsync method.


这篇关于如何在页面之间传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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