MvvmLight中CheckBeginInvokeOnUI方法解析,以及重写

1、CheckBeginInvokeOnUI的作用

在UI线程上执行操作。 如果从UI线程调用此方法,即刻执行该动作。 如果从另一个线程调用该方法,则该操作将排队在UI线程的分派器上并异步执行。 对于UI线程的其他操作,多亏了GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher属性,您可以获得对UI线程的分派器的引用。

2、使用方式

GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() => { Gui.Property = SomeNewValue; });

3、重写

using System;
using Windows.UI.Core;
using Windows.UI.Xaml;

namespace MyProject.Threading
{
    public static class DispatcherHelper
    {
        public static CoreDispatcher UIDispatcher { get; private set; }

        public static void CheckBeginInvokeOnUI(Action action)
        {
            if (UIDispatcher.HasThreadAccess)
                action();
            else UIDispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                       () => action());
        }

        static DispatcherHelper()
        {
            if (UIDispatcher != null)
                return;
            else UIDispatcher = Window.Current.Dispatcher;
        }
    }
}

4、使用方式

DispatherHelper.CheckBeginInvokeOnUI(() => UIUpdateMethod());

 参考文章:http://www.it1352.com/27272.html

发布了18 篇原创文章 · 获赞 6 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_28368039/article/details/103380515