Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Word没有调用我的AddIn´;s";“满负荷”;通过双击文档启动时的功能_C#_Ms Word_Vsto_Word Addins_Ribbonx - Fatal编程技术网

C# Word没有调用我的AddIn´;s";“满负荷”;通过双击文档启动时的功能

C# Word没有调用我的AddIn´;s";“满负荷”;通过双击文档启动时的功能,c#,ms-word,vsto,word-addins,ribbonx,C#,Ms Word,Vsto,Word Addins,Ribbonx,我为Word(C#,VSTO)编写了一个加载项。如果我在已启动的Word实例(Office 2016 Professional Plus)中打开Word文件,则会引发registered DocumentOpen事件。但是,如果我通过双击Word文件启动Word,则该事件将被忽略 我试图将函数Ribbon\u Load的代码放在ThisAddIn\u启动函数的ThisAddIn.cs类中。但行为仍然是一样的:当Word通过双击Wordfile启动时,不会引发事件DocumentOpen 以下是我

我为Word(C#,VSTO)编写了一个加载项。如果我在已启动的Word实例(Office 2016 Professional Plus)中打开Word文件,则会引发registered DocumentOpen事件。但是,如果我通过双击Word文件启动Word,则该事件将被忽略

我试图将函数Ribbon\u Load的代码放在ThisAddIn\u启动函数的ThisAddIn.cs类中。但行为仍然是一样的:当Word通过双击Wordfile启动时,不会引发事件DocumentOpen

以下是我的可复制示例:

Class
LAS Vorlagen.cs

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;

namespace LAS_Word_Vorlagen
{
    [ComVisible(true)]
    public class LAS_Vorlagen : Office.IRibbonExtensibility
    { 
        private Office.IRibbonUI ribbon;

        public LAS_Vorlagen()
        {
        }

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("LAS_Word_Vorlagen.LAS-Vorlagen.xml");
        }

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
            // Event-Listener registration
           // Globals.ThisAddIn.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(MessageBox_Show_At_Loading_Document);
        }

        public void MessageBox_Show_At_Loading_Document(Document doc)
        {
            MessageBox.Show("DocumentOpen-Event raised!");
        }

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }
    }
}
最后是XML配置
LAS Vorlagen.XML

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab id="TabAddIns_LAS"  label ="LAS-Vorlagen">
      </tab>
    </tabs>
  </ribbon>
</customUI>

如果要动态更新Fluent UI,需要使用功能区回调。对于外接程序实现的每个回调,都会缓存响应。例如,如果外接程序编写器为按钮实现了
getImage
回调过程,则调用该函数一次,加载图像,然后如果需要更新图像,则使用缓存的图像,而不是调用该过程。此过程保持不变,直到外接程序通过使用
Invalidate
方法发出缓存值无效的信号,此时再次调用回调过程并缓存返回响应。然后,外接程序可以通过调用
Refresh
方法强制立即更新UI

在这种情况下,以下方法可能会有所帮助:

然后,如果需要,可以调用上面指定的方法,通过调用功能区回调来更新缓存值:

this.ribbon.Invalidate()
在以下系列文章中阅读有关Fluent UI(也称为Ribbon UI)的更多信息:

仅供参考默认情况下,如果VSTO加载项尝试操作Microsoft Office用户界面(UI)但失败,则不会显示任何错误消息。但是,您可以将Microsoft Office应用程序配置为显示与UI相关的错误消息。您可以使用这些消息帮助确定自定义功能区不显示的原因,或功能区显示但不显示控件的原因

显示VSTO外接程序用户界面错误的步骤
  • 启动应用程序
  • 单击“文件”选项卡
  • 单击“选项”
  • 在类别窗格中,单击高级
  • 在“详细信息”窗格中,选择“显示VSTO以添加用户界面错误”,然后单击“确定”

  • 在文章中阅读更多关于这方面的内容

    与其说是一个bug,不如说是Word和Windows的设计方式。。。您可以尝试使用应用程序事件处理程序DocumentOpen吗?@Cindymister我已经使用了“DocumentOpen”处理程序,因为我在调用“Ribbon\u Load”函数时注册了它-请参见上面的示例。但“Ribbon_Load”函数仅在我在(空)Word实例中打开文档时调用。如果我通过双击wordfile打开文档,则不会调用“Ribbon_Load”。我知道这听起来很奇怪-特别是因为我的Ribbon一直在word中可用,但事件没有按应有的方式注册。不要在
    RibbonLoad
    中注册
    DocumentOpen
    ,在
    此加载项启动中注册它。这样,当文档打开时,您可以“轻推”功能区。@Cindymister不幸的是,这并没有按我所希望的那样进行。请看一看我上面问题中的更新2。问题中的信息没有以允许任何人进行测试的方式呈现-该问题缺少一个测试。然而,我能说的是,我不理解关于非静态函数的最后一句话。这是一个注册为处理
    DocumentOpen
    public void
    (意味着它不返回任何内容,因此不能成为函数),因此在VSTO加载项的当前实例中是否会自动执行?
    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
                this.ribbon = ribbonUI;  
    }
    
    this.ribbon.Invalidate()