将 VSTO 功能区公开给 C# 中的 VisibleChanged 事件
Expose VSTO Ribbon to VisibleChanged event in C#
我正在使用 C# VSTO 创建 Word 2010 的应用程序级加载项。当加载项的自定义任务窗格隐藏或可见时,我想访问加载项功能区的 get_Pressed
回调方法。但是,为了这样做,我需要使功能区可供 ThisAddIn
class 中的 myTaskPane_VisibleChanged
事件使用。我无法使用 Ribbons
集合,因为加载项中的功能区是在功能区 XML 中创建的,而不是使用 Visual Studio 的功能区设计器创建的。
在 ThisAddIn
class 我有:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myTaskPane.VisibleChanged += new EventHandler(myTaskPane_VisibleChanged);
}
和
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new myRibbon();
//I tried playing with IRibbonExtension here, but could not get that to work
}
和
public void myTaskPane_VisibleChanged(object sender, System.EventArgs e)
{
//Here is where I would like to access the ribbon
//I think the command would look something like:
//myRibbon.IsTaskPaneVisible = !myRibbon.IsTaskPaneVisible;
//myRibbon is not accessible here
}
在 myRibbon class 我有:
public class myRibbon : Office.IRibbonExtensibility
{
public Office.IRibbonUI ribbon;
private bool isTaskPaneVisible;
public bool IsTaskPaneVisible
{
get { return isTaskPaneVisible; }
set
{
isTaskPaneVisible = value;
ribbon.InvalidateControl("rxtglElementsPane");
}
}
和
public bool rxtglElementsPane_get_Pressed(Office.IRibbonControl control)
{
try
{
switch (control.Id)
{
case "rxtglElementsPane":
return isTaskPaneVisible;
default:
return false;
}
catch
{
return false;
}
}
}
大部分代码基于这篇文章:
Synchronizing Ribbon and Task Pane
作者在评论中提到 CreateRibbonExtensibilityObject
中生成的代码包括功能区的实例化。当我创建加载项时,Visual Studio 2013 没有生成此类代码。
对于从 ThisAddIn
class 访问功能区的任何帮助,我们将不胜感激。
无需访问功能区对象。相反,您可以使用 Globals.ThisAddin 属性 访问插件 class。有关详细信息,请参阅 Global Access to Objects in Office Projects。因此,您将能够修改可在功能区回调中使用的任何局部变量。
您可以在 MSDN 中的以下系列文章中阅读有关 Fluent UI(又名功能区 UI)的更多信息:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
MSDN Visual Studio 论坛的 Starain Chen 为我提供了 post 我放在那里的答案:
MSDN Expose VSTO Ribbon to VisibleChanged event in C# Answer
解决方案是用 myRibbon
类型定义 field/property 到 ThisAddIn
class。
public partial class ThisAddIn
{
internal myRibbon myRibbon;
...
protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
myRibbon=new myRibbon();
return myRibbon;
}
}
我正在使用 C# VSTO 创建 Word 2010 的应用程序级加载项。当加载项的自定义任务窗格隐藏或可见时,我想访问加载项功能区的 get_Pressed
回调方法。但是,为了这样做,我需要使功能区可供 ThisAddIn
class 中的 myTaskPane_VisibleChanged
事件使用。我无法使用 Ribbons
集合,因为加载项中的功能区是在功能区 XML 中创建的,而不是使用 Visual Studio 的功能区设计器创建的。
在 ThisAddIn
class 我有:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myTaskPane.VisibleChanged += new EventHandler(myTaskPane_VisibleChanged);
}
和
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new myRibbon();
//I tried playing with IRibbonExtension here, but could not get that to work
}
和
public void myTaskPane_VisibleChanged(object sender, System.EventArgs e)
{
//Here is where I would like to access the ribbon
//I think the command would look something like:
//myRibbon.IsTaskPaneVisible = !myRibbon.IsTaskPaneVisible;
//myRibbon is not accessible here
}
在 myRibbon class 我有:
public class myRibbon : Office.IRibbonExtensibility
{
public Office.IRibbonUI ribbon;
private bool isTaskPaneVisible;
public bool IsTaskPaneVisible
{
get { return isTaskPaneVisible; }
set
{
isTaskPaneVisible = value;
ribbon.InvalidateControl("rxtglElementsPane");
}
}
和
public bool rxtglElementsPane_get_Pressed(Office.IRibbonControl control)
{
try
{
switch (control.Id)
{
case "rxtglElementsPane":
return isTaskPaneVisible;
default:
return false;
}
catch
{
return false;
}
}
}
大部分代码基于这篇文章:
Synchronizing Ribbon and Task Pane
作者在评论中提到 CreateRibbonExtensibilityObject
中生成的代码包括功能区的实例化。当我创建加载项时,Visual Studio 2013 没有生成此类代码。
对于从 ThisAddIn
class 访问功能区的任何帮助,我们将不胜感激。
无需访问功能区对象。相反,您可以使用 Globals.ThisAddin 属性 访问插件 class。有关详细信息,请参阅 Global Access to Objects in Office Projects。因此,您将能够修改可在功能区回调中使用的任何局部变量。
您可以在 MSDN 中的以下系列文章中阅读有关 Fluent UI(又名功能区 UI)的更多信息:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
MSDN Visual Studio 论坛的 Starain Chen 为我提供了 post 我放在那里的答案:
MSDN Expose VSTO Ribbon to VisibleChanged event in C# Answer
解决方案是用 myRibbon
类型定义 field/property 到 ThisAddIn
class。
public partial class ThisAddIn
{
internal myRibbon myRibbon;
...
protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
myRibbon=new myRibbon();
return myRibbon;
}
}