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();
            }
        }
    }

ありがとう。

4

1 に答える 1

0

おそらく、WPF の下から Win32 ウィンドウを削除しているのでしょう。

私はすぐに試してみる準備ができていませんが、Close() 呼び出しを BeginInvoke() に変更してクローズをトリガーすると、thumbCancel_Click から処理が戻り、適切なタイミングで Close を処理できるようになると思います。

于 2013-01-11T20:35:18.487 に答える