简体   繁体   English

将 DispatcherHelper 从 MVVMLight 迁移到 Windows Community Toolkit

[英]Migrating DispatcherHelper from MVVMLight to Windows Community Toolkit

I am working on upgrading .NET 4.5 based WPF application to .Net 6. The current application uses DispatcherHelper which is not available in Windows Community Toolkit.我正在将基于 .NET 4.5 的 WPF 应用程序升级到 .Net 6。当前应用程序使用 Windows Community Toolkit 中不可用的DispatcherHelper I am unable to find any sample code which can help me to replace it with the ones available in WCT.我找不到任何可以帮助我将其替换为 WCT 中可用的示例代码。 I want to update the following code block with the one available in Windows Community Toolkit:我想用 Windows Community Toolkit 中可用的代码块更新以下代码块:

DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
    this.SelectedControl = new View.MainMenu();
});

and

DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
     this.SelectedControl = new View.Project();
});

You can find the source code for the DispatcherHelper here .您可以在此处找到DispatcherHelper的源代码。 In essence it does:本质上它确实:

  • Check if the action is valid, not null .检查操作是否有效,而不是null
  • Check if the calling thread is already the UI thread and if so, executes the action directly, otherwise queues it for asynchronous exectution.检查调用线程是否已经是 UI 线程,如果是,则直接执行操作,否则将其排队等待异步执行。
public static class DispatcherHelper
{
   public static void CheckBeginInvokeOnUI(Action action)
   {
      if (action == null)
         return;

      var dispatcher = Application.Current.Dispatcher;
      if (dispatcher.CheckAccess())
         action();
      else
         dispatcher.BeginInvoke(action);
   }
}

As far as I can see, there is only a replacement for DispatcherHelper for UWP in the toolkit, but not for WPF.据我所知,工具包中只有 UWP 的DispatcherHelper替代品,而 WPF 没有。 Either you replace the calls using your own DispatcherHelper type as above or you research where and in which context the method is used.您可以像上面那样使用自己的DispatcherHelper类型替换调用,或者研究使用该方法的位置和上下文。 You might as well be able to replace synchronous calls to the dispatcher with Invoke and asynchronous calls with InvokeAsync .您也可以用Invoke替换对调度程序的同步调用,用InvokeAsync替换异步调用。


The above version of the DispatcherHelper is cut to the essentials.上述版本的DispatcherHelper被删减到了基本要素。 In full, it would look like below for WPF.完整的,它看起来像下面的 WPF。 The only thing that it provides more is that you can manually initialize the dispatcher (plus check if it is alive), reset it, and as well as directly queueing an action for asynchronous execution on the dispatcher, but it might not be of any use for you.它提供的唯一更多功能是您可以手动初始化调度程序(加上检查它是否处于活动状态),重置它,以及直接将操作排队以在调度程序上异步执行,但它可能没有任何用处为你。

public static class DispatcherHelper
{
   public static Dispatcher UIDispatcher { get; private set; }

   public static void CheckBeginInvokeOnUI(Action action)
   {
      if (action == null)
         return;

      CheckDispatcher();

      if (UIDispatcher.CheckAccess())
         action();
      else
         UIDispatcher.BeginInvoke(action);
   }

   private static void CheckDispatcher()
   {
      if (UIDispatcher == null)
         throw new InvalidOperationException("The DispatcherHelper is not initialized.\n" +
                                             "Call DispatcherHelper.Initialize() in the static App constructor.");
   }

   public static DispatcherOperation RunAsync(Action action)
   {
      CheckDispatcher();
      return UIDispatcher.BeginInvoke(action);
   }

   public static void Initialize()
   {
      if (UIDispatcher != null && UIDispatcher.Thread.IsAlive)
         return;

      UIDispatcher = Dispatcher.CurrentDispatcher;
   }

   public static void Reset()
   {
      UIDispatcher = null;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM