Skip to content

Instantly share code, notes, and snippets.

@xdumaine
Created March 31, 2014 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdumaine/9900312 to your computer and use it in GitHub Desktop.
Save xdumaine/9900312 to your computer and use it in GitHub Desktop.
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