[转][C#] WPF 中 Application.DoEvents 的替代
参考:https://blog.csdn.net/qq_30725967/article/details/126379972
关于Application.DoEvents() :
此方法可以立即处理当前在消息队列中的所有 Windows 消息。 这样可以使界面不会出现假死的状况;但是WPF中没有Application.DoEvents()这个函数,可以使用以下方法替代:
public static class DispatcherHelper { [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] public static void DoEvents() { DispatcherFrame frame = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame); try { Dispatcher.PushFrame(frame); } catch (InvalidOperationException) { } } private static object ExitFrames(object frame) { ((DispatcherFrame)frame).Continue = false; return null; } }
这篇文章里的也可以,代码是一样的:https://www.cnblogs.com/JmlSaul/articles/6203364.html
在 ViewModel 构造函数中,使用以下代码才可以避免 Prism 错误 'The region manager does not contain the region' 错误:
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { region = regionManager.Regions["ContentRegion"]; }));
浙公网安备 33010602011771号