MvvmCross 5 导航服务中的片段初始化顺序
Fragment initialization order in MvvmCross 5 Navigation Service
我对 MvvmCross 5 中引入的导航服务有疑问。
在版本 4 中:
- 我使用
ShowViewModel<ViewModel>()
导航到片段
- 然后
Init
ViewModel 的方法被调用
- 之后调用 Fragment 的
OnCreateView
方法
- 在那里我可以根据 ViewModel 数据操作视图(例如向视图添加特定元素)。
在版本 5 中:
- 我用
await NavigationService.Navigate<ViewModel>()
导航
- Fragment 的
OnCreateView
首先被调用
- 之后是来自 ViewModel 的
Initialize
方法。
- 这在创建 Fragment 视图时以没有 ViewModel 数据结束。
这是异步导航的错误还是功能?
如果需要,是否有更好的方法来根据 ViewModel 数据操作 Fragment 视图?
Is this a bug or a feature of async navigation?
这是设计使然,但自 (v5.0.4) 以来已进行了修订,请参阅下面的流程更改。
If that is so wanted, is there a better way to manipulate the Fragment
view based on ViewModel data?
使用 v5.0.4+ 应该会产生您期望的行为。在 ViewModel
的 Initialize()
等待导航服务完成,然后开始视图生命周期事件。
MvvmCross v5.0.0 - v5.0.3
您看到的行为出现在 MvvmCross 5.0.0-5.0.3 中。流程如下:
ViewModel.Ctor
- (Selected Navigate calls)
Init(parameter)
(deprecated, uses reflection, rather use type safe初始化)
- (选定的导航调用)
ViewModel.ReloadState(savedState)
- (选定的导航调用)
ViewModel.Start()
BeforeNavigate
(NavigationService 事件)
*ViewDispatcher.ShowViewModel()
(触发器查看生命周期)
*ViewModel.Initialize()
AfterNavigate
(NavigationService 事件)
BeforeClose
(NavigationService 事件)
ViewDispatcher.ChangePresentation()
AfterClose
(NavigationService 事件)
MvvmCross v5.0.4+
v5.0.4+改进了流程并更改了导航顺序:
ViewModel.Ctor
BeforeNavigate
(NavigationService 事件)
*ViewModel.Initialize()
Init(parameter)
(已弃用,使用反射,而是使用类型安全初始化)
ViewModel.ReloadState(savedState)
ViewModel.Start()
*ViewDispatcher.ShowViewModel()
(触发器查看生命周期)
AfterNavigate
(NavigationService 事件)
BeforeClose
(NavigationService 事件)
ViewDispatcher.ChangePresentation()
AfterClose
(NavigationService 事件)
附加信息
您可以查看 GitHub 问题 (#1968) logged around the navigation order. Additionally, you can check out the pull request(#1971),它更新了 5.0.4 版本的 Initialize
订单。
我对 MvvmCross 5 中引入的导航服务有疑问。
在版本 4 中:
- 我使用
ShowViewModel<ViewModel>()
导航到片段 - 然后
Init
ViewModel 的方法被调用 - 之后调用 Fragment 的
OnCreateView
方法 - 在那里我可以根据 ViewModel 数据操作视图(例如向视图添加特定元素)。
在版本 5 中:
- 我用
await NavigationService.Navigate<ViewModel>()
导航 - Fragment 的
OnCreateView
首先被调用 - 之后是来自 ViewModel 的
Initialize
方法。 - 这在创建 Fragment 视图时以没有 ViewModel 数据结束。
这是异步导航的错误还是功能? 如果需要,是否有更好的方法来根据 ViewModel 数据操作 Fragment 视图?
Is this a bug or a feature of async navigation?
这是设计使然,但自 (v5.0.4) 以来已进行了修订,请参阅下面的流程更改。
If that is so wanted, is there a better way to manipulate the Fragment view based on ViewModel data?
使用 v5.0.4+ 应该会产生您期望的行为。在 ViewModel
的 Initialize()
等待导航服务完成,然后开始视图生命周期事件。
MvvmCross v5.0.0 - v5.0.3
您看到的行为出现在 MvvmCross 5.0.0-5.0.3 中。流程如下:
ViewModel.Ctor
- (Selected Navigate calls)
Init(parameter)
(deprecated, uses reflection, rather use type safe初始化) - (选定的导航调用)
ViewModel.ReloadState(savedState)
- (选定的导航调用)
ViewModel.Start()
BeforeNavigate
(NavigationService 事件)*ViewDispatcher.ShowViewModel()
(触发器查看生命周期)*ViewModel.Initialize()
AfterNavigate
(NavigationService 事件)BeforeClose
(NavigationService 事件)ViewDispatcher.ChangePresentation()
AfterClose
(NavigationService 事件)
MvvmCross v5.0.4+
v5.0.4+改进了流程并更改了导航顺序:
ViewModel.Ctor
BeforeNavigate
(NavigationService 事件)*ViewModel.Initialize()
Init(parameter)
(已弃用,使用反射,而是使用类型安全初始化)ViewModel.ReloadState(savedState)
ViewModel.Start()
*ViewDispatcher.ShowViewModel()
(触发器查看生命周期)AfterNavigate
(NavigationService 事件)BeforeClose
(NavigationService 事件)ViewDispatcher.ChangePresentation()
AfterClose
(NavigationService 事件)
附加信息
您可以查看 GitHub 问题 (#1968) logged around the navigation order. Additionally, you can check out the pull request(#1971),它更新了 5.0.4 版本的 Initialize
订单。