DispatcherHelper是MVVM Light用于解决线程内修改UI界面绑定值的一个工具类,常用于解决在线程内修改了值后界面需要实时的做出响应的情况。但是由于Mvvm Light对于.net 5.0的支持时间还比较短,这个类就包含没有在MVVMLightLibsStd10中。之前解决的方法都是尽量避免在线程中修改界面参数。
直到今天在博客看到有人直接把MvvmLightLibs中,DispatcherHelper类源码直接搬到了项目中,并且可以直接使用,特此记录,以供查阅。
using System;
using System.Text;
using System.Windows.Threading;
namespace WpfApp1.Common
{
/// <summary>
/// Helper class for dispatcher operations on the UI thread.
/// </summary>
public static class DispatcherHelper
{
/// <summary>
/// Gets a reference to the UI thread's dispatcher, after the
/// <see cref="M:GalaSoft.MvvmLight.Threading.DispatcherHelper.Initialize" /> method has been called on the UI thread.
/// </summary>
public static Dispatcher UIDispatcher { get; private set; }
/// <summary>
/// Executes an action on the UI thread. If this method is called
/// from the UI thread, the action is executed immendiately. If the
/// method is called from another thread, the action will be enqueued
/// on the UI thread's dispatcher and executed asynchronously.
/// <para>For additional operations on the UI thread, you can get a
/// reference to the UI thread's dispatcher thanks to the property
/// <see cref="P:GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher" /></para>.
/// </summary>
/// <param name="action">The action that will be executed on the UI
/// thread.</param>
public static void CheckBeginInvokeOnUI(Action action)
{
if (action == null)
return;
DispatcherHelper.CheckDispatcher();
if (DispatcherHelper.UIDispatcher.CheckAccess())
action();
else
DispatcherHelper.UIDispatcher.BeginInvoke((Delegate)action);
}
private static void CheckDispatcher()
{
if (DispatcherHelper.UIDispatcher == null)
{
StringBuilder stringBuilder = new StringBuilder("The DispatcherHelper is not initialized.");
stringBuilder.AppendLine();
stringBuilder.Append("Call DispatcherHelper.Initialize() in the static App constructor.");
throw new InvalidOperationException(stringBuilder.ToString());
}
}
/// <summary>Invokes an action asynchronously on the UI thread.</summary>
/// <param name="action">The action that must be executed.</param>
/// <returns>An object, which is returned immediately after BeginInvoke is called, that can be used to interact
/// with the delegate as it is pending execution in the event queue.</returns>
public static DispatcherOperation RunAsync(Action action)
{
DispatcherHelper.CheckDispatcher();
return DispatcherHelper.UIDispatcher.BeginInvoke((Delegate)action);
}
/// <summary>
/// This method should be called once on the UI thread to ensure that
/// the <see cref="P:GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher" /> property is initialized.
/// <para>In a Silverlight application, call this method in the
/// Application_Startup event handler, after the MainPage is constructed.</para>
/// <para>In WPF, call this method on the static App() constructor.</para>
/// </summary>
public static void Initialize()
{
if (DispatcherHelper.UIDispatcher != null && DispatcherHelper.UIDispatcher.Thread.IsAlive)
return;
DispatcherHelper.UIDispatcher = Dispatcher.CurrentDispatcher;
}
/// <summary>
/// Resets the class by deleting the <see cref="P:GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher" />
/// </summary>
public static void Reset()
{
DispatcherHelper.UIDispatcher = (Dispatcher)null;
}
}
}