如何追踪财产变化?

发布于 2024-09-26 12:30:53 字数 1046 浏览 2 评论 0原文

在 Viewmodel 中,我想跟踪异步调用的状态。假设我在视图模型中有两个异步调用,我想跟踪所有异步调用何时完成。 我做了如下: 设置为 private var 以跟踪每个异步调用:

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

设置一个属性,例如:

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

在每个异步调用的完成事件中,设置代码,例如: 对于调用 1:

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

对于调用 2:

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

然后我运行该应用程序,似乎 IsDone 的代码永远不会被触及。 如何解决这个问题或针对这种情况有更好的解决方案?

In Viewmodel, I want to track the status of async call. Suppose I have two async calls in viewmodel, I want to track when all async call done.
What I did as below:
Set to private var to track each async calls:

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:

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

For call 2:

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

Then I run the app, It seems code for IsDone never be touched.
How to reslove this problem or any better solution for this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-10-03 12:30:53

您的 IsDone 属性设计不佳。 setter 和 getter 彼此无关。您的应用在某些时候完全有可能将 IsDone 设置为 true,但 _isDone1 && _isDone2 评估为 false。这可能会导致微妙的错误。

当您需要在继续之前完成所有异步请求时,更好的通用方法是使用批加载器。它将批量处理所有异步请求,并在它们全部完成时触发一个事件。 这里是为 RIA 服务编写的批处理加载器,但如果您不使用 RIA 服务,一般概念仍然适用。

至于为什么你的“IsDone 代码从未被触及”,从你给出的代码片段中很难说出来。您正在引发正确的属性更改名称,因此您的应用程序中还发生了其他事情。也许发布更多代码?

在您发布的代码中,您从未调用 IsDone setter,因此据我们所知,setter 中用于向自己表示您已准备好继续的 if 语句永远不会被调用。

Your IsDone property is not well designed. The setter and getter have nothing to do with each other. It's entirely possible at some point for your app to set IsDone to true, but _isDone1 && _isDone2 evaluate to 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.

As for why your "code for IsDone is never touched", it's hard to say from the snippet you've given. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文