使用 Windows 功能区框架的应用程序关闭时崩溃





5.00/5 (2投票s)
使用 Windows 功能区框架的应用程序关闭时崩溃
既然我已经第三次被询问这个问题了,而且我自己也遇到了这个问题,我想我应该写篇博文来帮助未来的用户。
问题描述
你使用了Windows Ribbon Framework,无论是直接使用(在 C++ 中),还是使用我的Windows Ribbon for WinForms库在托管代码中使用。你添加了一个关闭按钮到 ribbon 上,该按钮关闭应用程序。应用程序在关闭时崩溃。
不要摧毁你所坐的树枝
问题在于你在 ribbon 命令处理程序中尝试调用 ribbon.DestroyFramework
,最终调用 IUIFramework.Destroy
。所以,在处理 ribbon 事件时,你尝试销毁 ribbon。Ribbon 控制器当然会反击。
解决方案
要么异步调用 Close()
方法
void _exitButton_OnExecute(
PropertyKeyRef key,
PropVariantRef currentValue,
IUISimplePropertySet commandExecutionProperties)
{
// Close form asynchronously since we are in a ribbon event
// handler, so the ribbon is still in use, and calling Close
// will eventually call _ribbon.DestroyFramework(), which is
// a big no-no, if you still use the ribbon.
this.BeginInvoke(new MethodInvoker(this.Close));
}
要么在关闭应用程序时不要调用 DestroyFramework
(并信任 Windows 来释放资源)。
【顺便说一句,C++ 解决方案是简单地调用 PostMessage(WM_CLOSE)
而不是 SendMessage(WM_CLOSE)
。】
我已经更新了项目网站上的示例 04-TabGroupHelp,使其在 ribbon 上有一个真正的退出按钮,可以关闭窗体。
暂时就到这里,
Arik Poznanski