小编Cri*_*erg的帖子

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

我在显示按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)

c# wpf asynchronous mvvm async-await

6
推荐指数
1
解决办法
792
查看次数

标签 统计

async-await ×1

asynchronous ×1

c# ×1

mvvm ×1

wpf ×1