在自定义功能区中显示其他信息

时间:2017-08-16 12:45:01

标签: c# xaml outlook outlook-addin add-in

今天我遇到了一个可以在我的日常工作中派上用场的想法。我想创建一个outlook加载项,显示发件人的用户ID(samAccountName)。我创建了一个新的Outlook加载项并尽我所能(我之前从未创建过)。

这就是目前的样子:

private void ThisAddIn_Startup(object sender, System.EventArgs e) {
            _explorer = Application.ActiveExplorer();
            _explorer.SelectionChange += _explorer_event;
            _userSearcher = new ActiveDirectoryUserSearcher();
        }

private void _explorer_event() {
            try {
                if (Application.ActiveExplorer().Selection.Count > 0) {
                    var selection = Application.ActiveExplorer().Selection[1];
                    if (selection is Outlook.MailItem) {
                        var item = selection as Outlook.MailItem;
                        var rec = item.Application.Session.CreateRecipient(item.SenderEmailAddress);
                        var user = rec.AddressEntry.GetExchangeUser();
                        var mail = user.PrimarySmtpAddress;
                        var user2 = _userSearcher.First($"(mail={mail})");

                        //here I'd have to pot some logic to set the text of the control, 
                        //such as "ribbon.EditBox.SetText("blabla");
                    }
                }
            }
            catch (Exception e) {
                Console.WriteLine(e);
                throw;
            }
        }

现在,要显示“内容”,我想创建一个新的Ribbon元素(也许有人可以判断是否可以显示其他信息,例如在邮件正文下方等等?)。我在项目中添加了一个新元素,并将其命名为“DisplayElement”。

在不同的互联网资源中,我试图找出如何处理这些控件并将以下内容添加到DisplayElement.cs中:

public string TextBoxText { get; set; }

        public DisplayElement()
        {
        }

        public string onGetText(Office.IRibbonControl control) {
            switch (control.Id.ToLower()) {
                case "user-id-text":
                    return TextBoxText;

            }
            return string.Empty;
        }

        public void onChange(Office.IRibbonControl control, string value) {
            switch (control.Id.ToLower()) {
                case "user-id-text":
                    TextBoxText = value;
                    break;
            }
            return;
        }

不幸的是现在我完全出局了。我无法弄清楚如何将editBox的文本设置为发件人User-Id。 我需要像“_ribbon.EditBox.SetText(”blabla“);”

DisplayElement XAML:

 <tabs>
      <tab idMso="TabAddIns">
        <group id="ContentGroup" label="Content">
          <button id="user-id-text" label="Insert Text"
                  screentip="Text" onAction="OnTextButton"
                  supertip="Inserts text at the cursor location."/>
          <editBox id="labelContent" getText="onGetText" onChange="onChange" enabled="false" screentip="User-ID" supertip="Displays the User-ID of the E-Mail Sender."/>

        </group>
      </tab>
    </tabs>

1 个答案:

答案 0 :(得分:0)

您可以使用Globals.Ribbons.MyRibbonName转到功能区。

请确保,MyRibbonName.cs设计人员的文件

partial class ThisRibbonCollection
{
    internal MyRibbonName MyRibbonName
    {
         // Getter
    }
}

然后,在您的ThisAddIn文件中,您可以执行以下操作:

public partial class ThisAddIn
{
    private MyRibbonName myRibb;
    private Explorer exp;

    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
        myRibb = Globals.Ribbons.MyRibbonName;
        exp = Application.ActiveExplorer();
        exp.SelectionChange += exp_SelectionChange;
    }
}

通过这种方式,您可以访问MyRibbonName功能区中的所有属性和元素,包括您在Q中寻找的EditBox

编辑:我以下列方式创建了我的功能区:

  1. 鼠标左键单击项目
  2. 选择添加&gt;新项目
  3. 选择功能区(可视设计器)
  4. MyRibbonName.Designer.cs中,partial class ThisRibbonCollection已经存在。

    您需要做的就是在功能区中添加可视元素并实现应用程序的业务逻辑。这非常类似于 Windows Forms 开发。

相关问题