在 MVVM 中实现临时覆盖的最佳方法
我正在编写一个连接到网络资源的应用程序。
当应用程序连接时,我想弹出一个覆盖层,其中包含通常的旋转进度图形和取消按钮。我为叠加层设计了一个 ConnectProgressViewModel 和匹配的 ConnectProgressView。
我的问题是从父 ViewModel 显示/隐藏覆盖层的最简洁方法是什么?
A) 从我的父 ViewModel 公开一个常量 ConnectProgressViewModel
,并让 ConnectProgressView
将其可见性绑定到 ConnectProgressViewModel.IsConnecting
> 财产。
B) 从父 ViewModel 公开通用 Overlay
属性,并在用户想要连接时将其设置为 ConnectProgressViewModel
。父视图将 ContentControl
绑定到此 Overlay
属性,数据模板负责处理其余部分。
C) ?
第一个似乎更多地封装了功能,应用程序不必关心显示和隐藏覆盖层,但当它只是偶尔显示时,始终暴露一个常量 ConnectProgressViewModel
感觉不对。
第二个似乎更适合 MVVM,仅在需要时才创建 ConnectProgressViewModel,但它将更多功能放在父级上,而且通用 Overlay 属性也感觉有点奇怪。
干杯
编辑:
我应该澄清,这个视图不仅仅显示忙碌状态。它允许取消/重试以及选择不同的网络资源等。为了简洁起见,我省略了这些细节,这可能是一个错误,因为人们专注于忙碌指示器。
I'm writing an app that connects to network resources.
When the app is connecting, I want to popup an overlay with the usual spinney progress graphic and a cancel button. I have designed a ConnectProgressViewModel
and matching ConnectProgressView
for the overlay.
My question is what is the cleanest way to show/hide the overlay from the parent ViewModel?
A) Expose a constant ConnectProgressViewModel
from my parent ViewModel, and have the ConnectProgressView
bind its visibility to the ConnectProgressViewModel.IsConnecting
property.
B) Expose a generic Overlay
property from the parent ViewModel, and set it to a ConnectProgressViewModel
when the user wants to connect. The parent View binds a ContentControl
to this Overlay
property and data templating takes care of the rest.
C) ?
The first seems to encapsulate the functionality more, with the app not having to care about showing and hiding the overlay, but exposing a constant ConnectProgressViewModel
all the time feels wrong when it's only show occasionally.
The second seems to fit MVVM better with the ConnectProgressViewModel
only being created when it's needed, but it places more functionality onto the parent, and also the generic Overlay property feels a bit weird too.
Cheers
EDIT:
I should clarify that this view does more than just show busy status. It allows cancelling/retries and selection of different network resources etc. I omitted such details for brevity which was perhaps a mistake as people are concentrating on the busy indicator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我总是只使用 Silverlight 工具包中的
BusyIndicator
。它没有取消按钮,但您可以将其设计为有一个。BusyIndicator
有一个IsBusy
属性,我将其绑定到 ViewModel 上的IsBusy
属性。如果您将控件设置为具有按钮,则可以向 ViewModel 添加取消命令。编辑
我刚刚看到这是WPF而不是Silverlight。我不确定 WPF 工具包是否有 BusyIndicator
再次编辑
它看起来像 扩展 WPF 工具包 有一个 BusyIndicator。请注意,我没有这方面的经验。
I always just use a
BusyIndicator
from the Silverlight Toolkit. It does not have a cancel button, but you can probably style it to have one. TheBusyIndicator
has anIsBusy
property that I bind to anIsBusy
property on my ViewModel. If you style the control to have a button, you can add a cancel command to your ViewModel.Edit
I just saw that this is WPF not Silverlight. I'm not sure if the WPF Toolkit has a BusyIndicator
Edit Again
It looks like the Extended WPF Toolkit has a BusyIndicator. Note, I have no experience with this.
我会同意您在 A) 中的建议,并认为您不应该实现像 B) 这样通用的东西,直到您实际上具有一定程度的灵活性作为要求,例如能够显示不同的覆盖视图。
保持简单!
I would go with something like your suggestion in A) and argue that you shouldn't implement something as generic like B) until you actually have that degree of flexibility as a requirement, like being able to show different overlay views.
Keep it simple!