public static class DispatcherHelper
{
public static Dispatcher UIDispatcher
{
get;
private set;
}
public static void CheckBeginInvokeOnUI(Action action)
{
if (UIDispatcher.CheckAccess())
{
action();
}
else
{
UIDispatcher.BeginInvoke(action);
}
}
public static void Initialize()
{
if (UIDispatcher != null)
{
return;
}
#if SILVERLIGHT
UIDispatcher = Deployment.Current.Dispatcher;
#else
UIDispatcher = Dispatcher.CurrentDispatcher;
#endif
}
}
1.添加调度器帮助静态类
DispatcherHelper
2. 程序初始化地方添加DispatcherHelper.Initialize();
3. 在非UI线程对界面元素进行修改时使用
DispatcherHelper.CheckBeginInvokeOnUI(()=>{});
也可使用
DispatcherHelper.UIDispatcher.BeginInvoke(()=>{});