简体   繁体   English

如何跟踪属性更改?

[英]How to track the property changed?

In Viewmodel, I want to track the status of async call. 在Viewmodel中,我想跟踪异步调用的状态。 Suppose I have two async calls in viewmodel, I want to track when all async call done. 假设我在viewmodel中有两个异步调用,我想跟踪所有异步调用何时完成。 What I did as below: Set to private var to track each async calls: 我所做的如下:设置为private var来跟踪每个异步调用:

private bool _isDone1 = false;
private bool _isDone2 = false;

Set one property like: 设置一个属性,例如:

 private bool _isDone;
 public bool IsDone
        {
            get { return this._isDone1&&this._isDone2; }
            set
            {
                if (this._isDone != value)
                {
                    this._isDone = value;                    
                    if(this._isDone)
                        {
                          // done somting here when all async call done
                        }
                    this.RaisePropertyChanged("IsDone");
                }
            }
        }

In completed event for each async call, set code like: For call 1: 在每个异步调用的完成事件中,设置代码如下:对于调用1:

_isDone1 = true;
this.RaisePropertyChanged("IsDone");

For call 2: 对于通话2:

_isDone2 = true;
this.RaisePropertyChanged("IsDone");

Then I run the app, It seems code for IsDone never be touched. 然后我运行该应用程序,似乎从未触及IsDone的代码。 How to reslove this problem or any better solution for this case? 如何解决此问题或针对此情况的任何更好的解决方案?

Your IsDone property is not well designed. 您的IsDone属性设计不正确。 The setter and getter have nothing to do with each other. setter和getter彼此无关。 It's entirely possible at some point for your app to set IsDone to true, but _isDone1 && _isDone2 evaluate to false. 您的应用有时可能IsDone设置为true,但是_isDone1 && _isDone2评估结果为false。 This may lead to subtle bugs. 这可能会导致细微的错误。

A better general approach when you need all of your async requests to be complete before proceeding is to use a batch loader. 在需要所有异步请求完成之前,一种更好的通用方法是使用批处理加载器。 It will batch up all your async requests and fire an event when they are all complete. 它将分批处理所有异步请求,并在所有请求完成后触发一个事件。 Here is a batch loader written for RIA Services, but if you aren't using RIA Services the general concept still applies. 这是为RIA Services编写的批处理加载器 ,但是如果您不使用RIA Services,则一般概念仍然适用。

As for why your "code for IsDone is never touched", it's hard to say from the snippet you've given. 至于为什么“从未触及IsDone的代码”,从给出的代码片段很难说出来。 You are raising the correct property change name, so there is something else going on in your app. 您正在提出正确的属性更改名称,因此您的应用中还有其他情况。 Perhaps post more code? 也许发布更多代码?

In the code you posted, you never call the IsDone setter, so as far as we know the if statement inside the setter that is used to signify to yourself you are ready to proceed never gets called. 在您发布的代码中,您永远不会调用IsDone setter,据我们所知,在setter中用于向自己表示您准备继续的if语句永远不会被调用。

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

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