【问题标题】:Outlook 2010 addin C# public methodsOutlook 2010 插件 C# 公共方法
【发布时间】:2011-11-27 10:56:25
【问题描述】:

我需要开发一个 Outlook 2010 插件,而且我是 Visual Studio 和 C# 的新手,因为我主要使用 PHP 和 JavaScript。我正在使用 Visual Studio 2010,并使用内置 Outlook 2010 加载项模板创建了一个项目。考虑以下代码:

// file ThisAddIn.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;


namespace OutlookAddIn1
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        public string displayCount()
        {
            Outlook.MAPIFolder inbox = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

            Outlook.Items unreadItems = inbox.Items.Restrict("[Unread]=true");

            return string.Format("Unread items in Inbox = {0}", unreadItems.Count);
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

// file Ribbon1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;

namespace OutlookAddIn1
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            // call ThisAddIn.displayCount() here
        }
    }
}

问题是,如何从 Ribbon1 类或其他任何地方的 ThisAddIn 类调用公共方法?我知道我需要一个对象引用,但是我怎样才能找到一个实例的名称呢?我看不到在现有文件的任何地方创建了 ThisAddIn 的实例。还是我误解了这个概念,应该以其他方式完成?我也将不胜感激有关创建 Office 加载项的任何建议或信息链接。

【问题讨论】:

    标签: c# visual-studio visual-studio-2010 outlook outlook-addin


    【解决方案1】:

    我使用在加载项初始化时设置的静态成员变量(带有关联的静态 getter):然后我可以从代码库中的任何位置以Core(根据需要选择名称)访问它。当然,如果加载项对象在上下文中可用,我会尝试传递它,但有时很难做到。

    该类由插件容器/加载器自动实例化(它实际上是作为 COM 组件公开的,至少在 ADX 中是这样工作的:)。

    编码愉快。


    代码可能如下所示:

    // inside ThisAddIn class
    public static ThisAddIn Active {
      get;
      private set;
    }
    
    // inside ThisAddIn_Startup
    Active = this;
    
    // later on, after add-in initialization, say in Ribbon1.button1_Click
    ThisAddIn.Active.displayCount();
    

    【讨论】:

      【解决方案2】:

      在 VSTO 项目中,可以在项目中的任何位置使用名为 Globals 的自动生成的密封类。 Globals 包含许多公共或内部静态属性,其中之一是 ThisAddIn(类型为 ThisAddIn,足够恰当)。代替上面的代码,您的代码将如下所示。

      在 Ribbon1.cs 中:

      public void DoSomethingOnRibbon(Office.IRibbonControl control)
      {
          string count = Globals.ThisAddIn.displayCount();
          ...
      }
      

      希望对您有所帮助。

      【讨论】:

      • 我在 ThisAddIn 类中有一个方法,我可以从同一个解决方案中的另一个项目访问它,但是在运行它时出现错误 Globals.ThisAddIn 为 null,我该如何解决这个问题跨度>
      猜你喜欢
      • 2011-08-27
      • 2012-10-26
      • 2011-11-23
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 2012-07-23
      • 2011-10-25
      相关资源
      最近更新 更多