Outlook加载项-运行时/从代码启用/禁用按钮 [英] Outlook Add-In - Enable/Disable Button during Runtime/from Code
问题描述
初始情况:
我们正在使用基于Framework 4.0,VSTO,DevExpress WinForm控件的VS.NET 2010开发C#中的Outlook 2010加载项.在Designer中,我们有一个功能区和一个RibbonTab,然后是RibbonGroup,然后是RibbonButton.我们正在从此Outlook加载项中使用WebServices.
We are developing an Add-in for Outlook 2010 in C# with VS.NET 2010 based on Framework 4.0, VSTO, DevExpress WinForm Controls. In Designer we have a Ribbon with a RibbonTab, then a RibbonGroup then a RibbonButton. We're consuming WebServices from within this Outlook Add-in.
目标:
当Web服务可用/不可用时(从代码中/从代码中退出),我们需要启用/禁用RibbonButtons
We need to enable/disable the RibbonButtons when the WebService is available/unavailable (from/out of the code)
我们找到了以下链接:
链接
功能区对象模型概述: http://msdn.microsoft.com/en -us/library/bb608623.aspx 功能区概述: http://msdn.microsoft.com/en-us/library/bb386097.aspx 演练:在运行时更新功能区上的控件: http://msdn.microsoft .com/en-us/library/bb608628.aspx
Ribbon Object Model Overview: http://msdn.microsoft.com/en-us/library/bb608623.aspx Ribbon Overview: http://msdn.microsoft.com/en-us/library/bb386097.aspx Walkthrough: Updating the Controls on a Ribbon at Run Time: http://msdn.microsoft.com/en-us/library/bb608628.aspx
经过数小时的尝试来弄清楚如何实现这一点,我们决定在SO上发布/询问这个问题.有人有示例代码吗?我们尝试了IRibbonExtensibility和CreateRibbonExtensibilityObject =>我们添加了RibbonTab,Group和Button并添加了对Click Event的订阅=>事件被触发但未处理(在button_Click(...)=> System.Diagnostics.Debugger.Break中()没有中断代码执行)
After hours of trying to figure out how to implement this we deciced to post/ask this question here on SO. Does anybody have a sample code? We tried the IRibbonExtensibility and CreateRibbonExtensibilityObject => we added the RibbonTab, Group and Button and added a subscription to the Click Event => The Event is fired but not handled (in button_Click(...) => System.Diagnostics.Debugger.Break() is not breaking the code execution)
谢谢!
基督徒
推荐答案
您将要以相当频繁的速度使功能区失效,以刷新每个选项卡/按钮的可见性.您可以通过订阅Click事件(完成操作)然后调用RibbonObject.Invalidate();
来实现.然后在每个按钮上添加一个getEnabled="yourTestFunction"
参数,并使用public bool yourTestFunction(Office.IRibbonControl control)
(在Ribbon.cs文件中定义)返回该Web服务是否可用.
You'll want to invalidate the Ribbon at a fairly frequent rate in order to refresh the visibility of each tab/button. You can do this by subscribing to the Click event (as you've done) and then calling RibbonObject.Invalidate();
. Then add a getEnabled="yourTestFunction"
parameter to each button, with public bool yourTestFunction(Office.IRibbonControl control)
(Defined in the Ribbon.cs file) returning whether the web service is available or not.
请记住,如果网络服务关闭,每次单击都会使您的应用程序挂起您在网络服务检查中设置的超时时间
修改:
刚刚意识到_Click事件没有映射到Excel COM库中,因此下面的代码将在每次更改单元格选择时运行(不如每次单击那样频繁,但希望足够好).
Just realized the _Click event isn't mapped in the Excel COM library, so here's a bit of code that will run each time the cell selection is changed (not as frequent as every click, but hopefully good enough).
ThisAddIn.cs
:
public static Excel.Application e_application;
public static Office.IRibbonUI e_ribbon;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
e_application = this.Application;
e_application.SheetSelectionChange += new Excel.AppEvents_SheetSelectionChangeEventHandler(e_application_SheetSelectionChange);
}
void e_application_SheetSelectionChange(object Sh, Excel.Range Target)
{
e_ribbon.Invalidate();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
e_application.SheetSelectionChange -= new Excel.AppEvents_SheetSelectionChangeEventHandler(e_application_SheetSelectionChange);
e_application = null;
}
Ribbon1.cs
:
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
ThisAddIn.e_ribbon = ribbonUI; //Add this line
}
和
public bool getEnabledTest(Office.IRibbonControl control)
{
//Whatever you use to test your Web Service
//return false;
}
Ribbon1.xml
:
<button id="WebService" label="Use The Web Service" onAction="executeWebService" getEnabled="getEnabledTest" />
这篇关于Outlook加载项-运行时/从代码启用/禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!