My demo application implements the MVVM pattern in a WPF project. The ViewModel calls a remote webservice via a service agent (proxy) like this:
proxy.GetProjectList((sender, e) => this.ProjectList, username, password);
ProjectList is a property defined in the ViewModel. It holds an array of CProject objects. The view binds to this property to display the project names. Basically this works fine.
However I get a NullReferenceException if I add the following if-statement:
proxy.GetProjectList((sender, e) => this.ProjectList = e.Result, username, password);
if (ProjectList.Length > 0) doSomething();
Debugging the application shows that the ProjectList property is null after the webservice has been called. And I just dont’t know why.
The webservice call above is implemented as follows:
public void GetProjectList(EventHandler<getProjectListCompletedEventArgs> callback, string username, string password) {
proxy.getProjectListCompleted += callback;
proxy.getProjectListAsync(username, password);
}
Following the hint from HighCore I changed my implementation to the following.
Calling the operation from the client:
Adding the callback method before:
Calling the actual webservice operation in a seperate service agent:
I don’t know if this is a good programming style but it works 🙂