MVVM - 视图加载和事件处理
在我的 Windows Phone 应用程序中,我需要跟踪一些事件以获得良好的流程。但我不确定如何按顺序处理它们。
应用程序启动时需要完成的操作:
- 加载主视图并实例化相应的视图模型
- 在视图模型的构造函数中,我启动一个登录序列,该序列在使用事件处理程序完成时发出信号
现在,当登录序列完成并且视图是完全加载我需要启动另一个序列。 但问题是,这两个事件“完成”的顺序并不总是相同...... 我使用 MVVMLight 中的 EventToCommand 向视图模型发出视图已“加载”的信号。
关于如何同步这个的任何想法。
In my windows phone app, I need to track some events to get a good flow. But I'm not sure how to handle them in good sequence.
What needs to be done at startup of the app:
- Main view is loaded and corresponding view model instantiated
- In the constructor of the view model I initiate a login sequence that signals when completed with an eventhandler
Now when the login sequence has finished AND the view is completely loaded I need to startup another sequence.
But here is the problem, the order of these 2 events 'completing' is not always the same...
I've use the EventToCommand from MVVMLight to signal the view model that the view has 'loaded'.
Any thoughts on how to synchronize this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您不应该在 UI 线程上使用等待句柄或类似的东西。您必须使用视图模型中的标志来同步这两种方法,并在继续之前检查它们。
因此,在视图模型中实现两个布尔属性。现在,当登录对话框完成时,将其中一个属性(我们称之为 IsLoggedIn)设置为 true,当初始化序列完成时,将另一个属性(IsInitialized)设置为 true。现在的技巧在于这两个属性的 setter 的实现:
或者,您可以从 setter 中删除
InitializationComplete
并将InitializationComplete
更改为:然后订阅 'PropertyChanged'事件使用以下实现:
As you should not use wait handles or something similar on the UI thread. You will have to sync the two method using flags in your view model and check them before progressing.
So, implement two boolean properties in your view model. Now when the login dialog is finished set one of the properties (lets call it IsLoggedIn) to true, and when the initialization sequence is finished you set the other property (how about IsInitialized) to true. The trick now lies in the implementation of the setter of these two properties:
Alternatively you can remove the
InitializationComplete
from the setters and changeInitializationComplete
to:Then subscribe to the 'PropertyChanged' event use the following implementation: