在为Windows 8.1 WinRT应用程序构建视图模型之一期间,我调用了DispatcherHelper.CheckBeginInvokeOnUI
我在运行时在App.xaml.cs OnLauched事件处理程序上初始化了DispatcherHelper.CheckBeginInvokeOnUI,但是在设计期间,当我调用DispatcherHelper.CheckBeginInvokeOnUI时,没有这样做,我得到了一个异常消息“DispatcherHelper未初始化”。
除了有条件地调用DistpatcherHelper、先检查ViewModelBase.IsInDesignMode之外,在设计期间有没有其他方法可以避免这个问题?
发布于 2015-06-15 22:33:37
正如问题中提到的,避免此问题的一种可能方法是首先检查我们是否处于设计模式,就像在这个要旨中所做的那样。
using System;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Threading;
namespace MvvmLight.Helpers
{
public class DesignAwareDispatcherHelper
{
public static void CheckDesignModeInvokeOnUI(Action action)
{
if (action == null)
{
return;
}
if (ViewModelBase.IsInDesignModeStatic)
{
action();
}
else
{
DispatcherHelper.CheckBeginInvokeOnUI(action);
}
}
}
}https://stackoverflow.com/questions/30855695
复制相似问题