有没有一种方法可以使用MVVM Light处理应用程序事件,如关闭,禁用,激活等?MVVM Light - WP7页面/应用程序事件
3
A
回答
4
你可以做的一件事就是在App.xaml.cs中处理这些事件,让他们使用默认的Messenger实例发送消息。然后,只需注册任何视图模型即可接收该消息。如果您需要取消该事件,请使用带回调的消息告诉应用程序取消。
5
感谢Matt Casto送我正确的方向。
这里是工作代码:
App.xaml.cs:
private void Application_Activated(object sender, ActivatedEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvents>(AppEvents.Activated, string.Empty));
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvents>(AppEvents.Deactivated, string.Empty));
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvents>(AppEvents.Closing, string.Empty));
}
视图模型:
Messenger.Default.Register<NotificationMessage<AppEvents>>(this, n =>
{
switch (n.Content)
{
case AppEvents.Deactivated:
_sessionPersister.Persist(this);
break;
case AppEvents.Activated:
var model = _sessionPersister.Get<TrackViewModel>();
break;
}
});
+0
是的,这工作。但是ApplicationLaunching呢?是可以强制从App.xaml.cs创建ViewModel? – 2013-05-27 22:52:31
相关问题
- 1. MVVM-Light事件怎么样?
- 2. MVVM Light - PivotControl
- 3. 如何使用MVVM Light在WPF应用程序的每个页面上触发相同的事件
- 4. 如何更改WP7应用程序的启动页面
- 5. WP7应用程序:太大页面
- 6. WP7应用程序滚动条页面
- 7. 超级简单的MVVM-Light WP7样本?
- 8. KendoUI MVVM Grid页面事件
- 9. MVVM Light太快:)
- 10. MVVM Light Messaging
- 11. 使用MVVM Light Toolkit的WPF应用程序
- 12. 在Windows Phone应用程序中一起使用MVVM Light和AgFx
- 13. 在Silverlight/WP7应用程序中使用MVVM样式模式
- 14. 使用MVVM处理应用程序事件的首选方法
- 15. MVVM Light:将事件绑定到后面代码中的命令
- 16. 在MVVM Light应用程序中实现编辑菜单
- 17. mvvm light PassEventArgsToCommand在ListBoxDragDropTarget中打破应用程序
- 18. 如何将Windows.UI.Composition动画集成到UWP MVVM Light应用程序
- 19. MVVM Light程序集与Microsoft.Practices.ServiceLocation冲突
- 20. MVVM Light Messaging问题
- 21. MVVM Light Toolkit示例
- 22. 使用MVVM Light中的ViewModels?
- 23. 使用MVVM/MVVM-Light编辑记录
- 24. WinRT MVVM Light示例项目
- 25. MVVM Light Binding
- 26. 如何使用MVVM Light设置WP7中的StackPanel的可见性?
- 27. WP7中如何在MVVM Light中使用ToggleSwitch?
- 28. MVVM Light EventToCommand在WP7 Pivot DataTemplate中不起作用
- 29. 使用MVVM Light绑定WPF图像控件事件EventToCommand
- 30. mvvm light - messaging
我喜欢它。谢谢! – chief7 2010-07-26 04:26:40