如何访问应用程序属性中VSTO Outlook加载ThisAddIn类以外的? [英] How to access Application property in VSTO Outlook add-in outside of ThisAddIn class?

查看:682
本文介绍了如何访问应用程序属性中VSTO Outlook加载ThisAddIn类以外的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的Outlook VSTO C#项目中创建ThisAddIn类有一个应用属性,您可以用它来除其他事项外获得访问Outlook文件夹和项目。问题是,你可以轻松地使用它时,你ThisAddIn类里面,但有没有方便地访问它从其他类项目。这是因为它是一个实例属性。

ThisAddIn class created with new Outlook VSTO C# project has a Application property that you can use to among other things get access to Outlook folders and items. The problem is that you can easily use it when you're inside of ThisAddIn class but there's no easy access to it from other classes in the project. This is because it's an instance property.

我要找到有机会获得相同的功能此属性在我的其他类提供,所以我想出了两个最好的方法可能的解决办法,但我不知道是哪一个(如果有的话),他们还是不错的。

I want to find the best way of having access to the same functionality this property provides in my other classes so I come up with two possible solutions but I don't know which one (if any) of them is good.

让我们假设我希望得到默认的收件箱文件夹。 ThisAddIn类里面我只想做这样的事情:

Lets assume I want to get default inbox folder. Inside ThisAddIn class I would simply do something like this:

this.Application.Session.GetDefaultFolder(Outlook.olFolderInbox);

现在怎么做同样的这个类之外?

Now how to do the same outside this class?

首先,我可以一个静态属性添加到的ThisAddIn 类,并将其设置为我想要的价值。暴露了其他类

First, I could add a static property to ThisAddIn class and set it to the value I want to expose in other classes.

public partial class ThisAddIn
{
    public Outlook.Application OutlookApp;

    void ThisAddIn_Startup(object sender, EventArgs e)
    {
        // init static variable value here
        OutlookApp = this.Application

        // initialize the rest of addin here
    }

    void InternalStartup()
    {
        this.Startup += this.ThisAddIn_Startup;
    }
}

这方法在我的任何其他类的我可以做是这样的:

This way in any of my other classes I could do something like this:

ThisAddIn.OutlookApp.Session.GetDefaultFolder(Outlook.olFolderInbox);



2。创建新应用程序对象



这是我可以做的第二件事是初始化Application对象我在其他课前我用它。但我不知道,如果创建类型不创建Outlook的新实例的新对象。

2. Create new Application object

Second thing that I could do is to init Application object in my other class before I use it. But I'm not sure if creating new object of that type doesn't create a new instance of Outlook.

class MyOtherClass
{
    public void MyMethod()
    {
        var app = new Outlook.Application();
        var folder = app.Session.GetDefaultFolder(Outlook.olFolderInbox);
    }
}



有没有人有哪些方法有什么建议较好,如果你有这个问题,我想这apprieciate以及不同的解决方案。

Does anyone have any suggestions which approach is better, of if you have different solutions for this problem I'd apprieciate that as well.

推荐答案

既然你可以有你的ThisAddIn单一实例可以有一个静态变量来访问外部申请表... FYI当您添加的Outlook的AddIn VSTO项目的ThisAddIn实例将作为静态类的全局

Since you can have single Instance of ThisAddIn you can have a static variable to access Application form outside... FYI when you add Outlook-AddIn VSTO project,instance of ThisAddIn will be available as static member in static class Globals

这篇关于如何访问应用程序属性中VSTO Outlook加载ThisAddIn类以外的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆