首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性更改启动长时间运行任务以填充另一个属性

属性更改启动长时间运行任务以填充另一个属性
EN

Stack Overflow用户
提问于 2014-12-12 14:57:42
回答 2查看 83关注 0票数 1

好的,我试着教自己MVVM模式和WPF,我遇到了一个块。

我有一个ViewModel,它有一个"SelectedProduct“字段。当设置该SelectedProduct字段时,我希望通过调用一个长时间运行的函数来填充另一个属性"BindedLimits“的内容(根据所选产品的不同,大约需要2-10秒)。理想情况下,我希望在后台启动更新,并在发生这种情况时以某种方式显示一个“进度”窗口,但我似乎找不到任何可靠的资源来说明我将如何完成这一任务,或者如果这是这样做的“正确”方式的话。

到目前为止我的ViewModel ..。

代码语言:javascript
复制
public class LimitsViewModel : PropertyChangedBase
{

    private ProductFamily selectedProduct;

    public ProductFamily SelectedProduct
    {
        get { return this.selectedProduct; }
        set
        {
            bool runLongOperation = true;

            if (value == this.selectedProduct)
            {
                runLongOperation = false;
            }

            this.SetPropertyChanged(ref this.selectedProduct, value);

            if (runLongOperation)
            {
                this.Limits = LoadLimits();
            }
        }
    }

    private ObservableCollection<BindedLimit> limits;

    public ObservableCollection<BindedLimit> Limits
    {
        get { return this.limits; }
        set
        {
            this.SetPropertyChanged(ref this.limits, value);
        }
    }

    private BindedLimit selectedLimit;

    public BindedLimit SelectedLimit
    {
        get { return this.selectedLimit; }
        set
        {
            this.SetPropertyChanged(ref this.selectedLimit, value);
        }
    }

    private ObservableCollection<BindedLimit> LoadLimits()
    {
        // Long running stuff here
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-12 15:34:14

有点像

代码语言:javascript
复制
private ProductFamily _selectedProduct;
public ProductFamily SelectedProduct
{
    get { return _selectedProduct; }
    set
    {
        this.SetPropertyChanged(ref _selectedProduct, value)
        Limits.Clear(); // or Limits = new ...
        Task.Run(() => LongTask());
    }
}

private void LongTask()
{
    var list = new List<BindedLimit>();
    ...
    App.Current.Dispatcher.Invoke(() => Limits = new ObservableCollection<BindedItems>(list));
}
票数 1
EN

Stack Overflow用户

发布于 2014-12-12 15:40:57

Sinatr与What's the best way to update an ObservableCollection from another thread?的链接帮助我找到了解决方案。这是我设计的。谢谢!

代码语言:javascript
复制
public class LimitsViewModel : PropertyChangedBase
{
    private CancellationTokenSource tokenSource;

    public LimitsViewModel()
    {
        this.tokenSource = new CancellationTokenSource();
    }

    public ICommand LoadCommand
    {
        get { return new RelayCommand(async x => await this.LoadLimits(this.tokenSource.Token)); }
    }

    private ProductFamily selectedProduct;
    public ProductFamily SelectedProduct
    {
        get { return this.selectedProduct; }
        set
        {
            this.SetPropertyChanged(ref this.selectedProduct, value);
            this.LoadCommand.Execute(null);
        }
    }

    private ObservableCollection<BindedLimit> limits;
    public ObservableCollection<BindedLimit> Limits
    {
        get { return this.limits; }
        set { this.SetPropertyChanged(ref this.limits, value); }
    }

    private bool limitsLoading;
    public bool LimitsLoading
    {
        get { return this.limitsLoading; }
        set { this.SetPropertyChanged(ref this.limitsLoading, value); }
    }

    private BindedLimit selectedLimit;
    public BindedLimit SelectedLimit
    {
        get { return this.selectedLimit; }
        set { this.SetPropertyChanged(ref this.selectedLimit, value); }
    }

    private async Task LoadLimits(CancellationToken ct)
    {
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27446297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档