使用 MVVM 对 ComboBox 选择进行异步方法调用

Cri*_*erg 6 c# wpf asynchronous mvvm async-await

我在显示按ComboBox选择过滤的图形时遇到问题,而无需锁定 UI。统计过滤非常繁重,需要运行async。在我尝试从属性设置器调用FilterStatisticsAsync和之前,这一切正常MonthSelectionChanged。有没有人有关于如何解决或解决这个问题的好提示?

XAML 看起来像这样:

        <ComboBox x:Name="cmbMonth"
                  ItemsSource="{Binding Months}" 
                  SelectedItem="{Binding SelectedMonth }"
                  IsEditable="True"
                  IsReadOnly="True"
Run Code Online (Sandbox Code Playgroud)

和 ViewModel 属性设置器是这样的:

    public string SelectedMonth
    {
        get { return _selectedMonth; }
        set { SetProperty(ref _selectedMonth, value); LoadStatisticsAsync(); MonthSelectionChanged(); }
    }
Run Code Online (Sandbox Code Playgroud)

SetProperty 派生自一个封装 INPC 的基类,如下所示:

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        protected virtual void SetProperty<T>(ref T member, T value, [CallerMemberName] string propertyName = null)
        {
            if (Equals(member, value))
                return;

            member = value;
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
Run Code Online (Sandbox Code Playgroud)

Dbl*_*Dbl 1

我会用这个来做:

    public class AsyncProperty<T> : INotifyPropertyChanged
    {
        public async Task UpdateAsync(Task<T> updateAction)
        {
            LastException = null;
            IsUpdating = true;

            try
            {
                Value = await updateAction.ConfigureAwait(false);
            }
            catch (Exception e)
            {
                LastException = e;
                Value = default(T);
            }

            IsUpdating = false;
        }

        private T _value;

        public T Value
        {
            get { return _value; }
            set
            {
                if (Equals(value, _value)) return;
                _value = value;
                OnPropertyChanged();
            }
        }

        private bool _isUpdating;

        public bool IsUpdating
        {
            get { return _isUpdating; }
            set
            {
                if (value == _isUpdating) return;
                _isUpdating = value;
                OnPropertyChanged();
            }
        }

        private Exception _lastException;

        public Exception LastException
        {
            get { return _lastException; }
            set
            {
                if (Equals(value, _lastException)) return;
                _lastException = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
Run Code Online (Sandbox Code Playgroud)

财产的定义

 public AsyncProperty<string> SelectedMonth { get; } = new AsyncProperty<string>();
Run Code Online (Sandbox Code Playgroud)

代码中的其他地方:

SelectedMonth.UpdateAsync(Task.Run(() => whateveryourbackground work is));
Run Code Online (Sandbox Code Playgroud)

在 xaml 中绑定:

SelectedItem="{Binding SelectedMonth.Value }"
Run Code Online (Sandbox Code Playgroud)

请注意,属性应反映当前状态,而不是触发可能需要无限时间的进程。因此,需要以与仅分配属性不同的方式更新属性。