异步回调内的属性设置不保持设置

时间:2011-01-06 14:54:50

标签: c# wcf silverlight-4.0 asynchronous

所以我有一个奇怪的错误,我希望有人可以帮助我。

我有以下代码从WCF RIA服务中获取一些实体,这是在Silverlight 4中,尽管我的猜测是没有区别的。

我错过了什么?

public class MyModel
{
    ...

    public IEnumerable<MyEntity> Result { get; private set; }

    public void Execute()
    {
        Context.Load(Query, LoadBehavior.RefreshCurrent, o =>
        {
            if (o.HasError)
            {
                ExecuteException = o.Error;
                if (ExecuteError != null)
                    ExecuteError(this, EventArgs.Empty);
                o.MarkErrorAsHandled();
            }
            else
            {
                //I've stepped through the code and the assignment is working
                //Result != null
                Result = o.Entities; 
                if (ExecuteSuccess != null)
                    ExecuteSuccess(this, EventArgs.Empty);
                //Inside any Handler of ExecuteSuccess
                //MyModel.Result == null
                //However I set a break point after ExecuteSuccess is triggered,
                //and once again MyModel.Result != null

            }

            if (ExecuteComplete != null)
                ExecuteComplete(this, EventArgs.Empty);

            ExecuteBusy = false;

        }, false);
    }
}

一切正常,直到我达到这一点:

MyModel.ExecuteSuccess += (o,e) => {
    //At this point MyModel.Result == null.  but why?
    var result = MyModel.Result;
};

1 个答案:

答案 0 :(得分:0)