Created
February 11, 2011 02:54
-
-
Save mikeminutillo/821846 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Threading; | |
namespace MagicUI.Framework | |
{ | |
public static class Execute | |
{ | |
[ThreadStatic] | |
private static bool _isUiThread; | |
private static Action<Action> _onUiExec = action => action(); | |
public static void OnUi(Action action) | |
{ | |
_onUiExec(action); | |
} | |
public static void SetUiContext(SynchronizationContext synchronizationContext) | |
{ | |
_onUiExec = action => | |
{ | |
if (_isUiThread) | |
action(); | |
else | |
synchronizationContext.Send(e => action(), null); | |
}; | |
OnUi(() => _isUiThread = true); | |
} | |
} | |
} |
This file contains hidden or 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
using System.ComponentModel; | |
namespace MagicUI.Framework | |
{ | |
public class NotifiesOfPropertyChangeOnUiThread : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged = delegate { }; | |
protected virtual void NotifyPropertyChanged(string propertyName) | |
{ | |
Execute.OnUi(() => PropertyChanged(this, new PropertyChangedEventArgs(propertyName))); | |
} | |
} | |
} |
This file contains hidden or 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
public void Run() | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
var rootWindow = new Window(); | |
// NOTE: Must be after the first form is created | |
Execute.SetUiContext(SynchronizationContext.Current); | |
var rootViewModel = CreateRootViewModel(); | |
rootWindow.SetViewModel(rootViewModel); | |
Application.Run(rootWindow); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment