使用 MVVM 和 Silverlight 的“等待"屏幕的一般首选方法 [英] Generally Preferred Method for a 'Wait' Screen using MVVM and Silverlight

查看:37
本文介绍了使用 MVVM 和 Silverlight 的“等待"屏幕的一般首选方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在推进一个小型的概念验证应用程序.这主要是为了加强我在 Silverlight 中的 MVVM 技能.我今天遇到了一个有趣的问题,我想不出如何解决 MVVM 的方式.我在搜索过程中也没有成功找到任何相关内容.

I'm moving along on a small proof of concept application. This is mostly to beef up my MVVM skills within Silverlight. I came across an interesting problem today that I could not figure how to solve the MVVM way. I wasn't successful finding anything relevant during search either.

问题来了,我有一个带有数据库后端的简单业务类型应用程序.我有一个登录视图和一个附加的视图模型,它将执行登录并报告成功或失败.没问题.我还不满意的是一种向用户报告等待屏幕的方法.因此,鉴于我的登录屏幕,用户单击登录,数据库聊天完成时会延迟几秒钟.我想报告此问题并在通话完成之前禁用任何交互.

So on to the problem, I have a simple business type application with a database back end. I have a login view and a view model attached that will perform the login and report success or failure. No problem. What I haven't been happy with just yet is a way to report a wait screen to the user. So given my login screen, the user click Login and there's a delay of a couple of seconds while the database chatting is done. I'd like to report this and disable any interaction until the call is completed.

我有几个想法.首先,将 Cursor 属性绑定到视图模型,VM 可以将 IsBusy 属性设置为 true.问题在于我似乎无法为 UserControl 绑定到 Cursor(Visual Studio 说 AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR).

I had a couple of ideas. First, bind the Cursor property to the viewmodel and the VM can set an IsBusy property to true. Problem with this is that I cannot seem to bind to Cursor for the UserControl (Visual Studio says AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR).

第二个想法是有一个等待屏幕.你知道,小齿轮转动或任何你想要的动画.这很好,但我不太清楚如何通过 Xaml 使视图在模型中切换.我知道我绝对可以连接事件并在代码中处理它.也许这就是要走的路?似乎有点违反 MVVM 粒度.

Second idea is to have a wait screen. You know, the little gears turning or whatever animation you want. And that's fine, but it's not real clear to me how I could make the view toggle this through the model through the Xaml. I know I could definitely hook up events and handle this in code. Maybe that's the way to go? Seems to against the MVVM grain just a bit.

对如何处理这个问题的更多想法感兴趣.

Would be interested in more ideas on how to handle this.

谢谢.

推荐答案

我们最终使用了一个服务来处理长时间运行的请求.该服务将工作负载作为委托,然后将其传递给 BackgroundWorker,同时打开我们的请稍候"视图.

We've ended up using a service for processing long running requests. The service takes the workload as a delegate, and then passes it off to a BackgroundWorker, while also opening our "Please wait" view.

这很有效,因为它允许我们以相同的方式通过一个相当简单的界面控制所有 ViewModel 中长时间运行的进程.

This works well as it allows us to control long running processes across all our ViewModels in the same way, with a fairly simple interface.

当您需要延迟时,您可以让来自 ViewModel 的事件更新视图,但随后您需要在所有 ViewModel 中使用此代码,而不是在可以更轻松维护的单个类中.

You could have events coming from the ViewModel update the View when you need a delay, but then you need to have this code in all your ViewModels, and not in a single class that can be maintained more easily.

编辑服务只是在您的 IOC 容器中注册的一个类,可供您的 ViewModel 使用.

EDIT A service is just a class that's registered within your IOC container, and can be used by your ViewModels.

public interface IProcessingService
{
    void Process(Action<BackgroundWorker> action);
    void Process(Action<BackgroundWorker> action, 
        Action<RunWorkerCompletedEventArgs> finish);
}

使用它,您的 ViewModel 可以实现类似的功能.

Using this your ViewModel could implement something like this.

{
    var processingService = container.Resolve<IProcessingService>();
    processingService.Process(worker => 
    {
        worker.ReportProgress(0, "Please wait...");
        // Do work here
        worker.ReportProgress(50);
        // Do more work
        worker.ReportProgress(100);
    });
}

这样,您用于显示进度通知的所有代码都在实现 IProcessingService 的类中,并且您的视图不会有任何直接控制视图或任何 UI 元素的代码.

This way all your code for displaying the progress notification is in the class that implements IProcessingService and your views remain free of any code that directly controls a view or any UI elements.

这篇关于使用 MVVM 和 Silverlight 的“等待"屏幕的一般首选方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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