在页面之间共享数据的最佳实践 [英] Best practice for sharing data between pages
问题描述
我想知道在UWP的页面之间发送变量(例如"selectedItem"等)的最佳实践是什么?仅创建每个Page都知道的静态全局变量类是一个好主意吗?
I was wondering what the best practice is for sending variables like 'selectedItem' and so on between pages in UWP? Is it a good idea to just create a static global variable class that every Page knows of?
推荐答案
我将在这里总结Microsoft 最佳实践 :
I'm going to sum up Microsofts Best Practice here:
用于简单数据(例如字符串):
使用 Frame.Navigate(TypeName, Object)
方法,其中第二种方法参数应始终为字符串(即使它允许对象).然后可以从NavigationEventArgs.Parameter
. aspx"rel =" noreferrer> Frame.Navigated
事件处理程序.
For simple data (like strings):
Use the Frame.Navigate(TypeName, Object)
method, where as the second argument should always be a string (even if it allows objects). The second argument can then be extracted from the NavigationEventArgs.Parameter
in the Frame.Navigated
event handler.
用于复杂数据(字符串除外):
您可以根据应用程序的大小和复杂程度在这里选择两个选项:
For complex data (anything besides strings):
You may choose from two options here, depending on the size and complexity of your app:
- 直接管理对
App
类中任何复杂数据的引用 - 或在属于
App
类成员的任何 Manager 类中保留对它们的引用. (例如NavigationDataManager
).
- Either manage a reference to any complex data inside your
App
class directly - Or keep a reference to them in any kind of Manager class, that is a member of your
App
class. (e.g.NavigationDataManager
).
这篇关于在页面之间共享数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!