我在消息处理程序中有以下代码(可以在任何线程上调用):
private readonly Dictionary<string,IView> _openedViews = new Dictionary<string,IView>();
private readonly object _lockObject = new object();
public MainView()
{
Messenger.Default.Register<ViewChangeMessage>(this, "adminView", m =>
{
var key = m.ViewName;
lock (_lockObject)
{
if (_openedViews.ContainsKey(key) == false)
_openedViews.Add(key, GetView(key));
content.Content = _openedViews[key];
}
//...
});
//...
我怎样才能得到这个异常:An element with the same key already exists in the System.Collections.Generic.Dictionary<TKey,TValue>.
如果我快速导致消息被多次发送,则会产生异常。
编辑:为代码添加了更多上下文,Messenger
来自 Galasoft.MVVMLight