gpt4 book ai didi

c# - Window.Loaded - ShowDialog() - 无法执行操作

转载 作者:行者123 更新时间:2023-12-02 05:13:05 24 4
gpt4 key购买 nike

描述

您好,我一直在网上搜索有关此问题的信息,但没有找到任何可以解决此问题的信息。

我想在加载主窗口时显示一个对话框,因此我为 MainWindow 的 Loaded 事件分配了一个事件处理程序。处理程序如下:

private void Window_Loaded(object sender, RoutedEventArgs e) {
if (Settings.Instance.GetBool(ConfigFunctionalityGroup, "CheckForUpdatesOnStartup")) {
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.WorkerReportsProgress = true;
worker.RunWorkerAsync();
}

findandreplacedialog = new FindAndReplaceDialog() { Owner = this };

Open_Environment_Click(null, null);
}

Open_Environment_Click 是一个菜单项的事件处理器,写法如下:

private void Open_Environment_Click(object sender, RoutedEventArgs e) {
SelectEnvironmentDialog dialog = new SelectEnvironmentDialog(Environments);

if (dialog.ShowDialog().Value == true) {
dialog.SelectedEnvironment.Open();
}
}

异常

Open_Environment_Click中调用dialog的ShowDialog时出现异常。

异常详情如下:

    System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Cannot perform this operation while dispatcher processing is suspended.
Source=WindowsBase
StackTrace:
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at System.Windows.Window.ShowDialog()
at UnrealSed.MainWindow.Open_Environment_Click(Object sender, RoutedEventArgs e) in c:\Users\ZéLuís\Documents\Visual Studio 2012\Projects\Unreal Sed\UnrealSed\MainWindow.xaml.cs:line 610
at UnrealSed.MainWindow.Window_Loaded(Object sender, RoutedEventArgs e) in c:\Users\ZéLuís\Documents\Visual Studio 2012\Projects\Unreal Sed\UnrealSed\MainWindow.xaml.cs:line 89
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
at MS.Internal.LoadedOrUnloadedOperation.DoWork()
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
at System.Windows.Interop.HwndTarget.OnResize()
at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
InnerException:

更多详情

有趣的是,当我在 MainWindow 的构造函数内的新窗口上调用 Show 时,我没有得到错误,维护上面的代码(只是在构造函数中添加这一行):

new GotoDialog().Show();

请注意,我没有缩短提供的任何代码,ShowDialog 不是自定义的,也不是在 Loaded 上打开窗口,而是从菜单中工作。

最佳答案

您可能遇到了这个问题,因为代码在 WPF 忙于计算布局时调用了 ShowDialog。在布局期间,调度程序处理被暂停,这意味着无法处理任何窗口管理器消息(因此您无法显示对话框,因为这样做依赖于正在处理的消息)。

通常解决这个问题的方法是简单地安排操作(在本例中为 ShowDialog),只要有可能就可以使用类似的东西

Dispatcher.CurrentDispatcher.BeginInvoke((Action)(...));

但是,您不能在此处执行此操作,因为您希望同步提供对话框的结果。而且您不能使用 Invoke,因为那样会引发错误或死锁(我还没有尝试过)。

您可以BeginInvoke 调用Open_Environment_Click 作为一个整体:

Dispatcher.CurrentDispatcher.BeginInvoke(
new Action(() => Open_Environment_Click(null, null)));

关于c# - Window.Loaded - ShowDialog() - 无法执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com