So I’ve got an odd bug I’m hoping someone can help me with.
I have the following code to grab some entities from WCF RIA Services, this is in Silverlight 4, though my guess is that doesn’t make a difference.
What am I missing?
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);
}
}
Everything works until I get to this point:
MyModel.ExecuteSuccess += (o,e) => {
//At this point MyModel.Result == null. but why?
var result = MyModel.Result;
};
Yeah I found the issue I was doing some crazy stuff in order to make MVVM + RIA Services more fun, in an inheriting class I was casting
MyModel.ResulttoMyModel.Result as IEnumerable<TEntity>;which doesn’t work. I usedMyModel.Result.OfType<TEntity>instead. I’m posting all the code in case it’s useful to someone else.Usage looks like this: