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

查看:20
本文介绍了错误窗口在 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 的样式,但您无法控制它.您能做的最好的事情就是关闭 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天全站免登陆