【转】【WPF】WPF强制刷新界面

Winform 里有 Application.DoEvents();可刷新!

WPF 里没这个,尽管可用委托实现多线程,但是刷新还是不行!

后来找到了 类似App.DoEvents()的方法();

代码:

public partial class App : Application
    {
        private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
        public static void DoEvents()
        {
            DispatcherFrame nestedFrame = new DispatcherFrame();
            DispatcherOperation exitOperation =  Dispatcher.CurrentDispatcher.BeginInvoke (DispatcherPriority.Background, exitFrameCallback, nestedFrame);
            Dispatcher.PushFrame(nestedFrame);
            if (exitOperation.Status !=
            DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }

        private static Object ExitFrame (Object state)
        {
            DispatcherFrame frame = state as
            DispatcherFrame;
            frame.Continue = false;
            return null;
        }

    }

}

 DispatcherFrame 的用途就是激活一新的消息循环,并以 Continue 属性作为退出新消息循环的标志。

原文地址:https://www.cnblogs.com/FengShenMeng/p/6054571.html

 

posted on 2021-08-11 16:07  梦琪小生  阅读(1933)  评论(0编辑  收藏  举报

导航