为什么 CustomTaskPane 抛出空异常

Why CustomTaskPane is throwing null exception

我正在创建 VSTO 加载项并且我已经尝试过 Add winform addin to Custom task pane in Word 但它抛出 NullReferenceException。

        CustomTaskPaneCollection CustomTaskPanes;
        CustomTaskPane ct;
        Pdfcontrol pdf = new Pdfcontrol();
        ct = Globals.ThisDocument.CustomTaskPanes.Add(pdf, "Pdf");

ThisDocument 没有 CustomTaskPanes 属性,这甚至不应该编译。你得到的答案是应用程序级插件,而你有一个文档级插件。在您的情况下,您想这样做:

Pdfcontrol pdf = new Pdfcontrol();
Globals.ThisDocument.ActionsPane.Controls.Add(pdf)