Created
March 31, 2014 19:29
-
-
Save xdumaine/9900312 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string username; | |
public string Username | |
{ | |
get | |
{ | |
return username; | |
} | |
set | |
{ | |
username = value; | |
RaisePropertyChanged("Username"); | |
} | |
} | |
// Assume this gets called from a command bound to a button, for example | |
public void UpdateUsername() | |
{ | |
var task = ThreadPool.RunAsync(t => | |
{ | |
// set the private property to prevent raising property changed, which has to be done on the UI thread | |
username = GetUsername(); | |
}); | |
task.Completed = RunOnComplete(RefreshUI); | |
} | |
public void RefreshUI() | |
{ | |
// set the public property to private one which raises property changed and triggers the UI to update | |
Username = username; | |
} | |
public AsyncActionCompletedHandler RunOnComplete(Action method) | |
{ | |
return new AsyncActionCompletedHandler((IAsyncAction source, AsyncStatus status) => | |
{ | |
DispatcherHelper.CheckBeginInvokeOnUI(() => { method(); }); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment