MVVM WPF中的错误窗口显示模态 [英] Error window show modal in MVVM WPF

查看:141
本文介绍了MVVM WPF中的错误窗口显示模态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的MVVM错误消息实现为一个消息对话框,该对话框通过调解器类订阅了错误消息,以便其他视图模型可以在发生任何错误时通知它.

I have implemented my MVVM error message as a message dialog that subscribes to error messages via a mediator class, so that other viewmodels can notify it if any errors occur.

发生错误时,我将viewmodel中的可见性属性设置为Visible,以显示错误窗口.所有这些都绑定在视图模型的错误"窗口中.

When an error occurs, I set the visibility attribute in the viewmodel to Visible, to display the error window. This is all bound in the Error window from the viewmodel.

但是,此窗口不是模态窗口!我需要将其显示为对话框,而不仅仅是将可见性设置为true-即使必须扩展窗口的功能,我也可以进行任何形式的绑定吗?如果可以避免的话,我宁愿不破坏MVVM.

However, this window is NOT modal! I need to show it as a dialog and not just set the visibility to true - is there any kind of binding I can do, even if I have to extend the functionality of the window? I'd rather not break MVVM if I can avoid it.

谢谢!

推荐答案

View/ViewModel拆分旨在将外观与功能分开.我坚信Window是一种功能,并且看起来可以合而为一.例如,如果在您的ErrorMessageViewModel中,您有发生以下错误时执行的代码:

The View/ViewModel split is meant to divide look from functionality. I firmly believe the Window is functionality and look rolled into one. For instance, what if in your ErrorMessageViewModel, you had this code that executes when there are errors:

class WindowViewModel : Window
{
}

.
.
.

WindowViewModel newDialog = new WindowViewModel();
newDialog.Content = myErrorListViewModel;
newDialog.Parent = mainWindowViewModel;
newDialog.ShowDialog();

因此,对话框的内容是您的错误列表的ViewModel.将您的View定义为自动将自身应用于错误列表ViewModel的数据模板.

So the contents of the dialog is the ViewModel for your error list. Define your View as a data template that automatically applies itself to the error list ViewModel.

这看起来不像MVVM吗?

Doesn't that look like MVVM?

事实是,Window类是您在屏幕上看到的Window的ViewModel.通过更改Window对象的属性,它会影响视图",就像将WindowView的属性绑定到WindowViewModel一样.唯一缺少的是使用WPF重设" Window的功能,并且尝试实现它的难度并不重要,您将无法做到这一点.用户可以通过修改其桌面主题来重新设置Window的样式,但是您无法控制它.最好的办法是关闭Chrome和/或使其全屏显示.

The fact is, the Window class is a ViewModel for the Window you see on the screen. By changing the properties of the Window object, it affects the "view" just like if the properties of the WindowView were bound to a WindowViewModel. The only thing missing is the ability to "restyle" the Window using WPF, and it doesn't matter how hard you try to implement it, you're not going to be able to do that. The user can restyle a Window by modifying their desktop theme, but you're not in control of it. The best you can do is turn off the chrome and/or make it full screen.

这篇关于MVVM WPF中的错误窗口显示模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆