My WPF application with the MVVM pattern shall basically perform the following:
- Button view binds to a command in the view model. –> Check!
- Command in view model asynchronously queries a webservice for a list of
CProjectobjects for putting it into aProjectListproperty. –> Check!
This looks like this…
Command in the view model:
proxy = new SomeService();
proxy.GetProjectList(GetProjectListCallback, username, password);
Callback in the view model:
private void GetProjectListCallback(object sender, GetProjectListCompletedEventArgs e) {
this.ProjectList = e.Result;
}
SomeService implements an interface ISomeService.
public void GetProjectList(EventHandler<GetProjectListCompletedEventArgs> callback, string username, string password) {
service.GetProjectListCompleted += callback;
service.GetProjectListAsync(username, password);
}
So far this works fine. However I feel that I would like to move this callback to the service itself so that the view model only calls something like:
proxy = new SomeService();
this.ProjectList = proxy.GetProjectList(username, password);
But when moving the callback to the service how could it return e.Result to the calling view model? Or would using a Task a better idea?
The easiest way is to re-create your WCF service proxy with VS2012. This will change your asynchronous method signatures to be something like:
and your command becomes:
If you don’t want to re-create your WCF service proxy (it will update all your method signatures), then you can wrap the
Begin*/End*methods as such:I have a full example of this kind of wrapping on my blog.
or the existing
*Async/*Completedmembers as such: