c# – 处理应用程序关闭来自Icon的调用

c# – 处理应用程序关闭来自Icon的调用,第1张

概述我在我的主窗口上添加了Closing事件的代码,用于点击X,它可以在没有问题的情况下取消事件(它通过“你确定吗?确保保存”类型对话框和闭幕活动) 不幸的是,如果我双击图标它会在RibbonWindow_Closing事件上点击我的断点,但是当e.Cancel设置为true时它无论如何都会关闭,就像它被Application.Current.Shutdown()调用一样. Alt-F4(和图标 – 我在我的主窗口上添加了Closing事件的代码,用于点击X,它可以在没有问题的情况下取消事件(它通过“你确定吗?确保保存”类型对话框和闭幕活动)

不幸的是,如果我双击图标它会在RibbonWindow_Closing事件上点击我的断点,但是当e.Cancel设置为true时它无论如何都会关闭,就像它被Application.Current.Shutdown()调用一样.

Alt-F4(和图标 – >关闭)和X按钮都正确处理,但没有双击图标本身

有谁知道为什么会这样?我正在使用Prism,如果重要的话,主窗口是由引导程序创建的.

这是堆栈跟踪,除了命中我的RibbonWindow_Closing事件之外的所有外部代码:

MyProgram.exe!MyProgram.Shell.RibbonWindow_Closing(object sender,System.ComponentModel.CancelEventArgs e) line 64  C# PresentationFramework.dll!System.windows.Window.OnClosing(System.ComponentModel.CancelEventArgs e) + 0x91 bytes
PresentationFramework.dll!System.windows.Window.WmClose() + 0x96 bytes      PresentationFramework.dll!System.windows.Window.WindowFilterMessage(system.intPtr hwnd,int msg,system.intPtr wParam,system.intPtr lParam,ref bool handled) + 0xe5 bytes  PresentationCore.dll!System.windows.Interop.HwndSource.PublicHooksFilterMessage(system.intPtr hwnd,ref bool handled) + 0x7e bytes     windowsBase.dll!MS.Win32.HwnDWrapper.WndProc(system.intPtr hwnd,ref bool handled) + 0xbe bytes    windowsBase.dll!MS.Win32.HwndSubclass.dispatcherCallbackOperation(object o) + 0x7d bytes    windowsBase.dll!System.windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,object args,int numArgs) + 0x53 bytes     windowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source,System.Delegate method,int numArgs,System.Delegate catchHandler) + 0x42 bytes    windowsBase.dll!System.windows.Threading.dispatcher.InvokeImpl(System.windows.Threading.dispatcherPriority priority,System.TimeSpan timeout,int numArgs) + 0xb4 bytes    windowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(system.intPtr hwnd,system.intPtr lParam) + 0x104 bytes    [Native to Managed Transition]  [Managed to Native Transition]  PresentationFramework.dll!System.windows.Window.InternalClose(bool shutdown,bool ignoreCancel) + 0xa1 bytes    PresentationFramework.dll!System.windows.Application.DoShutdown() + 0x1b6 bytes     PresentationFramework.dll!System.windows.Application.ShutdownImpl() + 0x1c bytes    PresentationFramework.dll!System.windows.Application.ShutdownCallback(object arg) + 0x5 bytes      windowsBase.dll!System.windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,int numArgs) + 0x53 bytes  windowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source,System.Delegate catchHandler) + 0x42 bytes    windowsBase.dll!System.windows.Threading.dispatcherOperation.InvokeImpl() + 0x8d bytes

测试RibbonWindow,给出消息但仍然关闭

<ribbon:RibbonWindow x:Class="MyProject.TestShell"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:ribbon="clr-@R_301_6889@space:Microsoft.windows.Controls.Ribbon;assembly=RibbonControlslibrary"        title="TestShell" Height="300" WIDth="300" Closing="Window_Closing">    <GrID>        <DockPanel LastChildFill="True">        </DockPanel>    </GrID></ribbon:RibbonWindow>

这作为常规窗口,获取消息并保持打开状态:

<Window x:Class="MyProject.TestShell"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        title="TestShell" Height="300" WIDth="300" Closing="Window_Closing">    <GrID>        <DockPanel LastChildFill="True">        </DockPanel>    </GrID></Window>

代码背后很简单:

private voID Window_Closing(object sender,System.ComponentModel.CancelEventArgs e){    e.Cancel = true;    MessageBox.Show("No close!");}

更新

好吧我把它缩小到RibbonWindow控件的问题,显然它在双击图标时关闭了应用程序.

显然,如果子窗口以相同的方式关闭,它也会关闭主应用程序:http://social.msdn.microsoft.com/Forums/en/wpf/thread/3e9cdc9c-dfb7-49f2-923a-ead07504d568

/// <summary>///   This handles the click events on the window icon./// </summary>/// <param @R_301_6889@="sender">Click event sender</param>/// <param @R_301_6889@="e">event args</param>private voID IconMouseleftbuttonDown(object sender,MousebuttonEventArgs e){    if (e.ClickCount == 1)    {        if (SystemCommands.ShowSystemMenuCommand.CanExecute(null,this))        {            SystemCommands.ShowSystemMenuCommand.Execute(null,this);        }    }    else if (e.ClickCount == 2)    {        if (ApplicationCommands.Close.CanExecute(null,this))        {            ApplicationCommands.Close.Execute(null,this);        }    }}
解决方法 好吧我把它缩小到RibbonWindow控件的问题,显然它在双击图标时关闭了应用程序.

http://social.msdn.microsoft.com/Forums/en/wpf/thread/3e9cdc9c-dfb7-49f2-923a-ead07504d568

/// <summary>///   This handles the click events on the window icon./// </summary>/// <param @R_301_6889@="sender">Click event sender</param>/// <param @R_301_6889@="e">event args</param>private voID IconMouseleftbuttonDown(object sender,this);        }    }}

我在这里找到了解决方案:http://social.msdn.microsoft.com/Forums/en/wpf/thread/9955b191-13d5-4986-a5c0-e73f50a44b44

这是注册我自己的ApplicationClosing命令,如:

[Export]public partial class TestShell : RibbonWindow{    public TestShell()    {        InitializeComponent();        CommandManager.RegisterClassCommandBinding(typeof(TestShell),new CommandBinding(ApplicationCommands.Close,CloseApplicationExecuted));    }    private voID Window_Closing(object sender,System.ComponentModel.CancelEventArgs e)    {        e.Cancel = true;        MessageBox.Show("Not closing 1!");    }    private static voID CloseApplicationExecuted(object sender,ExecutedRoutedEventArgs args)    {        RibbonWindow window = sender as RibbonWindow;        if (window != null)        {            MessageBox.Show("Not closing 2!");            args.Handled = true;        }    }}

现在,如果我双击图标我得到“不关闭2!”,以及任何其他关闭方法,我得到“不关闭1!”

希望这可以节省一些时间让我想出来的时间.谢谢Hans帮我诊断问题.

更新:如果您希望CloseApplicationExecuted触发与常规关闭相同的事件,请调用

private static voID CloseApplicationExecuted(object sender,ExecutedRoutedEventArgs args)    {        RibbonWindow window = sender as RibbonWindow;        if (window != null)        {            args.Handled = true;            window.Close();        }    }
总结

以上是内存溢出为你收集整理的c# – 处理应用程序关闭来自Icon的调用全部内容,希望文章能够帮你解决c# – 处理应用程序关闭来自Icon的调用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存