在页面之间共享数据的最佳实践
Best practice for sharing data between pages
我想知道在 UWP 中的页面之间发送 'selectedItem' 等变量的最佳做法是什么?只创建一个每个页面都知道的静态全局变量 class 是个好主意吗?
事实上,如果您使用 MVVM 方法,您会在 ModelView class(es) 中获得所有必要的信息。如果您不使用 MVVM,只需使用单例 class 甚至静态全局 class.
我将在此处总结 Microsoft 最佳实践:
对于简单数据(如字符串):
使用 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
事件处理程序。
对于复杂数据(字符串以外的任何数据):
您可以在此处从两个选项中进行选择,具体取决于您应用的大小和复杂程度:
- 直接管理对
App
class 中任何复杂数据的引用
- 或者在您的
App
class 的任何类型的 Manager class 中保留对它们的引用。 (例如 NavigationDataManager
)。
我想知道在 UWP 中的页面之间发送 'selectedItem' 等变量的最佳做法是什么?只创建一个每个页面都知道的静态全局变量 class 是个好主意吗?
事实上,如果您使用 MVVM 方法,您会在 ModelView class(es) 中获得所有必要的信息。如果您不使用 MVVM,只需使用单例 class 甚至静态全局 class.
我将在此处总结 Microsoft 最佳实践:
对于简单数据(如字符串):
使用 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
事件处理程序。
对于复杂数据(字符串以外的任何数据):
您可以在此处从两个选项中进行选择,具体取决于您应用的大小和复杂程度:
- 直接管理对
App
class 中任何复杂数据的引用 - 或者在您的
App
class 的任何类型的 Manager class 中保留对它们的引用。 (例如NavigationDataManager
)。