简体   繁体   English

使用MVVM模式在UserControl WPF中异步加载

[英]Loading asynchronously in a UserControl WPF with MVVM pattern

I have a small problem async loader in a view with WPF MVVM pattern . 我在使用WPF MVVM模式的视图中有一个小问题异步加载程序。 I wish that when I click the button Read from UserControl View , send a Command to view model , and then it was off a loading before making execute and it disappeared when I return data . 我希望当我单击“从UserControl视图中读取”按钮时,发送一个Command来查看模型,然后在执行执行之前将其卸载,并在返回数据时消失。 The data is read by a machine via optical USB . 机器通过光学USB读取数据。 All works , the program runs perfectly, just can not bring up the loading so async . 所有工作正常,程序运行完美,只是无法异步加载。 The loading is displayed along with the return of reading , because synchronous . 由于同步,负荷与读数返回一起显示。 How can I do asynchronous ? 我该如何做异步操作? I tried it with the task but it seems he does not consider the code . 我尝试了此任务,但似乎他不考虑代码。

class  ReadAndPrintFromDevice : ICommand
    {
        async Task<int> showLoader()
        {
           model.ShowLoader = true;                         
           return 1;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {                    
            return true;
        }

        public async void Execute(object parameter)
        {
            showLoader async here
            //other code..
            showloader async shadow here, after other code
        }
    }

if you need more information tell in the comments that I add all . 如果您需要更多信息,请在评论中告诉我添加全部。

I would suggest something like this: 我建议这样的事情:

public class ReadAndPrintFromDeviceAsyncCommand : PropertyChangedBase, ICommand
{
    private bool _IsBusy;

    public bool IsBusy
    {
        get { return _IsBusy; }
        private set
        {
            _IsBusy = value;
            NotifyOfPropertyChange();
        }
    }

    public bool CanExecute(object parameter)
    {
        return !IsBusy;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public async void Execute(object parameter)
    {
        IsBusy = true;

        await ...

        IsBusy = false;
    }
}

By using an IsBusy property, you can simply set that to be true before executing the asynchronous operation, and once it's done, set it back to false. 通过使用IsBusy属性,您可以在执行异步操作之前简单地将其设置为true然后将其设置回false。

You can then bind your control to the IsBusy property: 然后,您可以将控件绑定到IsBusy属性:

<MyControl Visibility="{Binding SomeCommand.IsBusy, ...}"

The above example would require a BooleanToVisibilityConverter to work, but I think you get the idea. 上面的示例需要一个BooleanToVisibilityConverter才能工作,但我认为您明白了。

Also, the command uses a PropertyChangedBase which I have created to simply make implementing INotifyPropertyChanged a bit easier, the code for this class is here: 另外,该命令使用我创建的PropertyChangedBase来简化实现INotifyPropertyChanged的过程,该类的代码如下:

public abstract class PropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyOfPropertyChange([CallerMemberName]string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM