ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2122|回复: 1

[求助] 用XML方式创建Ribbon,但调用时显示其他toggleButton未定义。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2017-12-24 00:12 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace XML1
  7. {
  8.     public static class Share
  9.     {
  10.         public static Microsoft.Office.Tools.CustomTaskPane ctp1;
  11.     }
  12. }
复制代码
以上Share.cs内容
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  3.   <ribbon>
  4.     <tabs>
  5.       <tab id="martins" label="martins">
  6.         <group id="MyGroup" label="My Group">
  7.           <toggleButton id="toggleButton1" label="显示/隐藏" onAction="toggleButton1_Click" size="large"/>
  8.         </group>
  9.       </tab>
  10.     </tabs>
  11.   </ribbon>
  12. </customUI>
复制代码
以上RibbonXML1.XML内容
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using Office = Microsoft.Office.Core;
  9. using System.Windows.Forms;
  10. using Microsoft.Office.Tools.Ribbon;

  11. // TODO:    按照以下步骤启用功能区(XML)项:

  12. // 1. 将以下代码块复制到 ThisAddin、ThisWorkbook 或 ThisDocument 类中。

  13. //  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
  14. //  {
  15. //      return new RibbonXML1();
  16. //  }

  17. // 2. 在此类的“功能区回调”区域中创建回调方法,以处理用户
  18. //    操作(如单击某个按钮)。注意:  如果已经从功能区设计器中导出此功能区,
  19. //    则将事件处理程序中的代码移动到回调方法并修改该代码以用于
  20. //    功能区扩展性(RibbonX)编程模型。

  21. // 3. 向功能区 XML 文件中的控制标记分配特性,以标识代码中的相应回调方法。  

  22. // 有关详细信息,请参见 Visual Studio Tools for Office 帮助中的功能区 XML 文档。


  23. namespace XML1
  24. {
  25.     [ComVisible(true)]
  26.     public class RibbonXML1 : Office.IRibbonExtensibility
  27.     {
  28.         private Office.IRibbonUI ribbon;

  29.         public RibbonXML1()
  30.         {
  31.         }

  32.         #region IRibbonExtensibility 成员

  33.         public string GetCustomUI(string ribbonID)
  34.         {
  35.             return GetResourceText("XML1.RibbonXML1.xml");
  36.         }

  37.         #endregion

  38.         #region 功能区回调
  39.         //在此创建回调方法。有关添加回调方法的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=271226

  40.         public void Ribbon_Load(Office.IRibbonUI ribbonUI)
  41.         {
  42.             this.ribbon = ribbonUI;
  43.         }
  44.         //public void boa(Office.IRibbonControl control)
  45.         //{
  46.         //    if (control.Id == "button1")
  47.         //    {
  48.         //        MessageBox.Show("You clicked " + control.Id);
  49.         //    }
  50.         //    else
  51.         //    {
  52.         //        MessageBox.Show("You clicked a different control.");
  53.         //    }
  54.         //}
  55.         public void toggleButton1_Click(object sender, RibbonControlEventArgs e)
  56.         {
  57.             Share.ctp1.Visible = this.toggleButton1.Checked;
  58.         }
  59.         #endregion

  60.         #region 帮助器

  61.         private static string GetResourceText(string resourceName)
  62.         {
  63.             Assembly asm = Assembly.GetExecutingAssembly();
  64.             string[] resourceNames = asm.GetManifestResourceNames();
  65.             for (int i = 0; i < resourceNames.Length; ++i)
  66.             {
  67.                 if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
  68.                 {
  69.                     using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
  70.                     {
  71.                         if (resourceReader != null)
  72.                         {
  73.                             return resourceReader.ReadToEnd();
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.             return null;
  79.         }

  80.         #endregion
  81.     }
  82. }
复制代码
以上为RibbonXML1.cs内容
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using Word = Microsoft.Office.Interop.Word;
  7. using Office = Microsoft.Office.Core;
  8. using Microsoft.Office.Tools.Word;

  9. namespace XML1
  10. {
  11.     public partial class ThisAddIn
  12.     {
  13.         UserControl1 uc1;
  14.         private void ThisAddIn_Startup(object sender, System.EventArgs e)
  15.         {
  16.             uc1 = new UserControl1();
  17.             Share.ctp1 = this.CustomTaskPanes.Add(uc1, "审计数据搜索");
  18.             Share.ctp1.Visible = true;
  19.             Share.ctp1.VisibleChanged += new EventHandler(ctp1_visibleChanged);//为任务窗格创建事件。
  20.             Share.ctp1.DockPositionChanged += new EventHandler(ctp1_DockPositionChanged);
  21.         }
  22.         private void ctp1_visibleChanged(object sender, System.EventArgs e)
  23.         {
  24.             //对应的事件过程。
  25.             RibbonXML1 ribbon = Globals.Ribbons.GetRibbon<RibbonXML1>(); //获得功能区可视化设计器中的Ribbon
  26.             ribbon.toggleButton1.Checked = Share.ctp1.Visible;
  27.         }
  28.         private void ctp1_DockPositionChanged(object sender, System.EventArgs e)
  29.         {
  30.             Globals.ThisAddIn.Application.StatusBar = Share.ctp1.DockPosition.ToString();
  31.         }
  32.         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
  33.         {
  34.         }
  35.         protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
  36.         {
  37.             return new RibbonXML1();
  38.         }
  39.         #region VSTO 生成的代码

  40.         /// <summary>
  41.         /// 设计器支持所需的方法 - 不要
  42.         /// 使用代码编辑器修改此方法的内容。
  43.         /// </summary>
  44.         private void InternalStartup()
  45.         {
  46.             this.Startup += new System.EventHandler(ThisAddIn_Startup);
  47.             this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
  48.         }
  49.         
  50.         #endregion
  51.     }
  52. }
复制代码
以上为ThisAddin.cs内容
其中这句,ribbon.toggleButton1.Checked = Share.ctp1.Visible; 显示未定义,该如何定义呢?

TA的精华主题

TA的得分主题

发表于 2018-1-12 16:47 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
为什么不上传项目文件呢,这样让人不方便帮你查看.
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-23 20:47 , Processed in 0.039165 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表