【发布时间】:2017-07-28 22:31:23
【问题描述】:
我需要为每个使用过的 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 对象在调用卸载之前不会一直有效。为了可靠地识别 MailItem,我使用了EntryID。现在的问题是,这仅在保存项目时才有效。
所以目前如果没有找到EntryID,我会保存该项目。但这会自动将项目保存在草稿下。
有没有办法区分MailItem 没有以某种方式保存以便可以在Dictionary<,> 中使用。
【问题讨论】:
-
您可以创建并设置一个UserProperty 来存储唯一ID。
-
我不需要保存该项目以保留 UserProperty 吗?
-
如果邮件项没有被用户存储和保存,则ID保持未使用状态。但这应该不是问题。