使用列表实现

时间:2018-04-12 09:55:58

标签: c# .net

我正在尝试使用C#制作PowerPoint插件,每次打开或创建新演示文稿时,都会添加一个面板。 我对面板的实例化存在问题 我可以同时打开几个演示文稿,所以我尝试制作一个这样的面板列表:

 public partial class ThisAddIn
    {
        private UserControlA myControl1;
        private UserControlA myControl2;
        private UserControlA myControl3;
        private UserControlA myControl4;
        private UserControlA myControl5;
        private List<UserControlA> myControlX;

        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane1;  
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane2;
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane3;
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane4;
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane5;
        private List<Microsoft.Office.Tools.CustomTaskPane> myCustomTaskPaneX;

        int iNbOfInstances = 0;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            myControl1 = new UserControlA();
            myControl2 = new UserControlA();
            myControl3 = new UserControlA();
            myControl4 = new UserControlA();
            myControl5 = new UserControlA();

            List<UserControlA> myControlX = new List<UserControlA> {
                myControl1, myControl2, myControl3, myControl4, myControl5
            };

            List<Microsoft.Office.Tools.CustomTaskPane> myCustomTaskPaneX = new List<Microsoft.Office.Tools.CustomTaskPane> {
                myCustomTaskPane1, myCustomTaskPane2, myCustomTaskPane3, myCustomTaskPane4, myCustomTaskPane5
            };

            // event for new presentation
            ((EApplication_Event)this.Application).NewPresentation +=
                new PowerPoint.EApplication_NewPresentationEventHandler(
                Application_NewPrez);

            // event for presnetation opening
            this.Application.PresentationOpen +=
                new PowerPoint.EApplication_PresentationOpenEventHandler(
                Application_NewPrez);
        }


        private void Application_NewPrez(PowerPoint.Presentation Prez)
        {
            if (iNbOfInstances >= 0 && iNbOfInstances <= 10)
            {

                myCustomTaskPaneX[iNbOfInstances] = this.CustomTaskPanes.Add(myControlX[iNbOfInstances], "Addin Name");
                DisplayPanel();

                iNbOfInstances++;
            }
        }

        private void DisplayPanel()
        {
            for (int i = 0; i <= iNbOfInstances; i++)
                myCustomTaskPaneX[iNbOfInstances].Visible = true;
        }

当我启动PowerPoint时,我在行

时出错
myCustomTaskPaneX[iNbOfInstances] = this.CustomTaskPanes.Add(myControlX[iNbOfInstances], "Addin Name");

说引用未定义为myControlX为null

您知道我的代码中存在什么问题吗? PS:我尝试过调试器,当PowerPoint启动时,myControlX不为空,但只要我在PowerPoint欢迎页面中选择一个演示文稿,它就会变为空。

1 个答案:

答案 0 :(得分:0)

改变这个:

function scroll(element, speed) {
    element.animate({ scrollTop: $(document).height() }, speed,'linear', function() {
        $(this).animate({ scrollTop: 0 }, speed, scroll(element, speed));
    });
}

scroll($('#start, #end'), 300000);

要:

List<UserControlA> myControlX = new List<UserControlA> {
    myControl1, myControl2, myControl3, myControl4, myControl5
};

List<Microsoft.Office.Tools.CustomTaskPane> myCustomTaskPaneX = new List<Microsoft.Office.Tools.CustomTaskPane> {
    myCustomTaskPane1, myCustomTaskPane2, myCustomTaskPane3, myCustomTaskPane4, myCustomTaskPane5
};

这样就可以了解&#34; myControlX&#34; (和&#34; myCustomTaskPaneX&#34;)是类级别,而不是过程级别。