从WPF中的ViewModel类(MVVM模式)更新UI [英] Update UI from ViewModel class (MVVM pattern) in WPF

查看:588
本文介绍了从WPF中的ViewModel类(MVVM模式)更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第一个WPF应用程序中使用的是MVVM模式,但我认为基本的东西都存在问题.

I'm using the MVVM pattern in my first WPF app and have a problem with something quite basic I assume.

当用户在我的视图上单击保存"按钮时,将执行一条命令,该命令调用ViewModel中的私有void Save().

When the user hits the "save" button on my view, a command gets executed that calls the private void Save() in my ViewModel.

问题在于"Save()"中的代码需要花费一些时间来执行,因此我想在执行大量代码之前在UI视图中隐藏保存"按钮.

The problem is that the code in "Save()" takes some time to execute, so I'd like to hide the "Save" button in the UI view before executing the large chunk of code.

问题在于,只有在视图模型中执行了所有代码之后,视图才会更新. 在执行Save()代码之前,如何强制视图重绘和处理PropertyChanged事件?

The problem is that the view doesn't update untill all code is executed in the viewmodel. How can I force the view to redraw and process the PropertyChanged events before executing the Save() code?

此外,我想使用一种可重用的方法,以便我也可以轻松地在其他页面中执行相同的操作.其他人已经做了类似的事情吗?是否显示正在加载..."消息?

Additionally, I would like a reuseable way, so that I can easily do the same thing in other pages as well.. Anyone else made something like this already? A "Loading..." message?

推荐答案

如果需要很长时间,请考虑使用单独的线程,例如使用BackgroundWorker,以便UI线程可以保持响应(即更新).用户界面).

If it takes a long time, consider using a separate thread, for example by using a BackgroundWorker, so that the UI thread can stay responsive (i.e. update the UI) while the operation is performed.

在您的Save方法中,您将

  • 更改UI(即,修改绑定到UI的一些INotifyPropertyChanged或DependencyProperty IsBusySaving布尔值,隐藏保存"按钮,并可能显示带有IsIndeterminate = True的某些进度条)和
  • 启动BackgroundWorker.
  • change the UI (i.e. modify some INotifyPropertyChanged or DependencyProperty IsBusySaving boolean which is bound to your UI, hides the Save button and maybe shows some progress bar with IsIndeterminate = True) and
  • start a BackgroundWorker.

在BackgroundWorker的DoWork事件处理程序中,进行冗长的保存操作.

In the DoWork event handler of your BackgroundWorker, you do the lengthy saving operation.

在UI线程中执行的RunWorkerCompleted事件处理程序中,将IsBusySaving设置为false,并可能更改UI中的其他内容以表明操作已完成.

In the RunWorkerCompleted event handler, which is executed in the UI thread, you set IsBusySaving to false and maybe change other stuff in the UI to show that you are finished.

代码示例(未经测试):

Code example (untested):

BackgroundWorker bwSave;
DependencyProperty IsBusySavingProperty = ...;

private MyViewModel() {
    bwSave = new BackgroundWorker();

    bwSave.DoWork += (sender, args) => {
        // do your lengthy save stuff here -- this happens in a separate thread
    }

    bwSave.RunWorkerCompleted += (sender, args) => {
        IsBusySaving = false;
        if (args.Error != null)  // if an exception occurred during DoWork,
            MessageBox.Show(args.Error.ToString());  // do your error handling here
    }
}

private void Save() {
    if (IsBusySaving) {
        throw new Exception("Save in progress -- this should be prevented by the UI");
    }
    IsBusySaving = true;
    bwSave.RunWorkerAsync();
}

这篇关于从WPF中的ViewModel类(MVVM模式)更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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