Я создал Outlook Addin (используя VIsual Studio 2017, в C # используя VSTO) и добавил диалог страницы параметров для ввода учетных данных пользователя.
Это диалоговое окно параметров отображается, когда пользователь переходит в Outlook -> "Параметры -> Дополнительно -> Надстройки-> Выбирает надстройку, а затем нажимает кнопку" Параметры надстройки ", как показано на снимке экрана ниже:
Теперь я хочу программно открыть эту OptionsPage из кода AddIn.
Я уже пытался создать экземпляр 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);
}
}
}