使用MVVM调用ComboBox时的异步方法

时间:2017-08-08 15:26:25

标签: c# wpf asynchronous mvvm async-await

我在显示由ComboBox选项过滤的图表时遇到问题,而没有用户界面锁定。统计过滤非常繁重,需要运行async。这一切都很好,直到我尝试从Property setter中调用FilterStatisticsAsyncMonthSelectionChanged。有没有人对如何解决或解决这个问题有很好的建议?

XAML看起来像这样:

        <ComboBox x:Name="cmbMonth"
                  ItemsSource="{Binding Months}" 
                  SelectedItem="{Binding SelectedMonth }"
                  IsEditable="True"
                  IsReadOnly="True"

ViewModel属性设置器如下:

    public string SelectedMonth
    {
        get { return _selectedMonth; }
        set { SetProperty(ref _selectedMonth, value); LoadStatisticsAsync(); MonthSelectionChanged(); }
    }

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));
        }

1 个答案:

答案 0 :(得分: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));
        }
    }

财产的定义

 public AsyncProperty<string> SelectedMonth { get; } = new AsyncProperty<string>();

代码中的其他地方:

SelectedMonth.UpdateAsync(Task.Run(() => whateveryourbackground work is));
xaml中的

绑定:

SelectedItem="{Binding SelectedMonth.Value }"

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