无法使用 ThumbnailToolBarButton 在窗口上调用 Close

发布于 2024-12-10 10:27:01 字数 3325 浏览 0 评论 0原文

我正在尝试通过单击 ThumbnailToolBarButton 按钮来关闭全新 WPF 应用程序中的窗口(默认生成的 xaml 没有更改)。当我尝试这样做时,出现以下错误:

    System.InvalidOperationException was unhandled
    Message=Operation is not valid due to the current state of the object.
    Source=WindowsBase
    StackTrace:
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.NativeWindow.WndProc(Message& m)
   at Microsoft.WindowsAPICodePack.Taskbar.ThumbnailToolbarProxyWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at WpfApplication1.App.Main() in d:\documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
   InnerException: 

这是我的代码:

    using System;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Interop;
    using Microsoft.WindowsAPICodePack.Taskbar;

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                Loaded += new RoutedEventHandler(MainWindow_Loaded);
            }

            void  MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                ThumbnailToolBarButton thumbCancel = new ThumbnailToolBarButton(System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location), "Cancel");

                thumbCancel.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbCancel_Click);

                TaskbarManager.Instance.ThumbnailToolBars.AddButtons(new WindowInteropHelper(this).Handle, thumbCancel);
            }

            void thumbCancel_Click(object sender, ThumbnailButtonClickedEventArgs e)
            {
                Close();
            }
        }
    }

谢谢。

I am trying to close a Window in a brand new WPF application (no changes to the default generated xaml) via button click of a ThumbnailToolBarButton. When I try to do so, I am presented with the following error:

    System.InvalidOperationException was unhandled
    Message=Operation is not valid due to the current state of the object.
    Source=WindowsBase
    StackTrace:
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.NativeWindow.WndProc(Message& m)
   at Microsoft.WindowsAPICodePack.Taskbar.ThumbnailToolbarProxyWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at WpfApplication1.App.Main() in d:\documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
   InnerException: 

Here is my code:

    using System;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Interop;
    using Microsoft.WindowsAPICodePack.Taskbar;

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                Loaded += new RoutedEventHandler(MainWindow_Loaded);
            }

            void  MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                ThumbnailToolBarButton thumbCancel = new ThumbnailToolBarButton(System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location), "Cancel");

                thumbCancel.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbCancel_Click);

                TaskbarManager.Instance.ThumbnailToolBars.AddButtons(new WindowInteropHelper(this).Handle, thumbCancel);
            }

            void thumbCancel_Click(object sender, ThumbnailButtonClickedEventArgs e)
            {
                Close();
            }
        }
    }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2024-12-17 10:27:01

也许您正在从 WPF 下删除 Win32 窗口。

我还没有准备好尝试它,但我敢打赌,如果您将 Close() 调用更改为 BeginInvoke() 以触发关闭,那么它将允许处理从thumbCancel_Click 返回并在适当的时间处理关闭。

Perhaps you're deleting the Win32 window out from underneath WPF.

I am not setup to readily try it but I'd wager that if you change your Close() call to a BeginInvoke() to trigger the close then it will allow processing to return from the thumbCancel_Click and process the Close at a proper time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文