我需要为每个使用的MailItem存储一个模型。为此我写了下面的方法唯一标识Mailitem
private readonly static Dictionary<string, PermitCustomPaneViewmodel> ViewmodelLookup = new Dictionary<string, PermitCustomPaneViewmodel>();
public static PermitCustomPaneViewmodel CreateOrGet(MailItem c)
{
if (c.EntryID == null)
c.Save();
if (!ViewmodelLookup.ContainsKey(c.EntryID))
{
var vm = new PermitCustomPaneViewmodel(c);
c.Unload +=() => ViewmodelLookup.Remove(c.EntryID);
ViewmodelLookup.Add(c.EntryID, vm);
}
return ViewmodelLookup[c.EntryID];
}
当模型已经存在,我查找并返回它。如果它没有被创建,我创建它并在MailItem被卸载后删除条目。
但是我观察到MailItem
对象不会一直被调用直到卸载被调用。为了可靠地识别我使用的邮件项目EntryID
。现在的问题是这只有在保存项目时才有效。
所以目前我保存该物品,如果没有找到EntryID
。但是这会自动将项目保存在草稿中。
有没有办法distingush MailItem
的不保存的方式,因此它可以用于Dictionary<,>
。
您可以创建并设置[UserProperty](https://www.add-in-express.com/creating-addins-blog/2011/08/19/how-to-add-a-custom-property -to-the-userproperties-collection-of-an-e-mail-item-in-outlook /)来存储唯一ID。 –
我是否还需要保存该项目以支持UserProperty? – lokimidgard
如果邮件项目未被用户存储和保存,该ID仍未被使用。但这不应该是一个问题。 –