首页 > 解决方案 > VSTO:如何以编程方式打开我的 AddIn 的 OptionsPage

问题描述

我创建了 Outlook 插件(使用 VIsual Studio 2017,在 C# 中使用 VSTO)并添加了一个选项页面对话框来输入用户凭据。

当用户转到 Outlook ->“选项 -> 高级 -> 插件 -> 选择插件然后点击“插件选项按钮”时,将显示此选项对话框,如下面的屏幕截图所示: 在此处输入图像描述 现在我想以编程方式打开此选项页插件代码。

我已经尝试创建 OptionsPage 的实例并调用 Show() 方法。但它不会出现。

以下不起作用,即使代码已执行,它只是不显示 OptionsPage 对话框:

MyAppOptionPage optionsPage = new myAppOptionPage();
optionsPage.Show();

我的 OptionsPage 类定义如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using RestSharp;

namespace OutlookAddIn4
{
    [ComVisible(true)]
    public partial class MyAppOptionPage : UserControl, Outlook.PropertyPage
    {
        #region Private Variables

        /// <summary>
        /// This is the parent PropertyPageSite Object.
        /// If we want to enable the Apply Button, we must call the
        /// OnStatusChange() and the PropertyPageSIte Object will check our Dirty Flag. 
        /// </summary>
        Outlook.PropertyPageSite _PropertyPageSite = null;

        /// <summary>
        /// This is our Statusvariable indicating that our Options has changed.
        /// </summary>
        bool _Dirty = false;

        #endregion

        const int captionDispID = -518;

        public MyAppOptionPage()
        {
            InitializeComponent();
            this.Load += new EventHandler(MyAppOptionPage_Load);            
        }

        void MyAppOptionPage_SettingsChange(object sender, EventArgs e)
        {
            OnDirty(true);
        }

        void MyAppOptionPage_Load(object sender, EventArgs e)
        {
            _PropertyPageSite = GetPropertyPageSite();
            // preset the settings with the current user 
            Properties.Settings.Default.user = ThisAddIn.GetSmtpAddress(Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry);
            this.login.Text = Properties.Settings.Default.user;
            this.password.Text = ""; 
            this.serverurl.Text = Properties.Settings.Default.serverurl;
        }

        #region PropertyPage Members

        /// <summary>
        /// This function is called when the user clicks "Apply" or "OK".
        /// </summary>
        public void Apply()
        {
            if (_Dirty)
            {
                // Save Settings
                Properties.Settings.Default.user = this.login.Text;
                // Properties.Settings.Default.token already set in the login process
                Properties.Settings.Default.serverurl = this.serverurl.Text;                
                Properties.Settings.Default.Save();
                // Reset Dirty Flag
                OnDirty(false);
            }
        }

        /// <summary>
        /// This function gets the parent PropertyPageSite Object using Reflection.
        /// Must be called in Load event.
        /// </summary>
        /// <returns>The parent PropertyPageSite Object</returns>
        Outlook.PropertyPageSite GetPropertyPageSite()
        {
            Type type = typeof(System.Object);
            string assembly = type.Assembly.CodeBase.Replace("mscorlib.dll", "System.Windows.Forms.dll");
            assembly = assembly.Replace("file:///", "");

            string assemblyName = System.Reflection.AssemblyName.GetAssemblyName(assembly).FullName;
            Type unsafeNativeMethods = Type.GetType(System.Reflection.Assembly.CreateQualifiedName(assemblyName, "System.Windows.Forms.UnsafeNativeMethods"));

            Type oleObj = unsafeNativeMethods.GetNestedType("IOleObject");
            System.Reflection.MethodInfo methodInfo = oleObj.GetMethod("GetClientSite");
            object propertyPageSite = methodInfo.Invoke(this, null);

            return (Outlook.PropertyPageSite)propertyPageSite;
        }


        public void OnDirty(bool dirty)
        {
            _Dirty = dirty;
            _PropertyPageSite.OnStatusChange();
        }

        /// <summary>
        /// The Dirty Property used form parent PropertyPageSite object.
        /// </summary>
        public bool Dirty
        {
            get { return _Dirty; }
        }

        /// <summary>
        /// The GetPageInfo function used form parent PropertyPageSite object,
        /// when user requests help.
        /// </summary>
        /// <param name="HelpFile">The name of the Helpfile.</param>
        /// <param name="HelpContext">The Index of the HelpContext requested.</param>
        public void GetPageInfo(ref string HelpFile, ref int HelpContext)
        {
            // TODO: Implement Helpfile
        }

        #endregion

        bool Outlook.PropertyPage.Dirty
        {
            get
            {
                return _Dirty;
            }
        }
        void Outlook.PropertyPage.GetPageInfo(ref string helpFile, ref int helpContext)
        {

        }

        [DispId(captionDispID)]
        public string PageCaption
        {
            get
            {
                return "MyApp Settings";
            }
        }

        private void Login_Click(object sender, EventArgs e)
        {
            OnDirty(true);
            try
            {                
                ThisAddIn.myMyAppAPI = new MyAppAPI(ThisAddIn.GetSmtpAddress(Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry), this.serverurl.Text, this.login.Text, this.password.Text);
                this.testresult.Text = "Login succcessfull!";
            }
            catch (ApplicationException exception)
            {
                this.testresult.Text = "Login unsuccessfull.Check your parameters above! "+exception.Message;        
            }        
        }

        private void textChanged(object sender, EventArgs e)
        {
            // TODO is textChanged the correct event to handle for key changes
            OnDirty(true);
        }
    }
}

标签: c#vsto

解决方案


推荐阅读