2015-06-15 18 views
2

期间建设了Windows Phone 8.1的WinRT应用查看车型之一,我在App.xaml.cs OnLauched事件处理程序DispatcherHelper.CheckBeginInvokeOnUIMVVM光调度辅助设计时错误

打电话我在运行时初始化DispatcherHelper ,但在设计时这个初始化没有这样做,当我打电话DispatcherHelper.CheckBeginInvokeOnUI,我得到一个异常与消息“DispatcherHelper未初始化”

有什么办法避免比调用DistpatcherHelper条件等在设计时这个问题,先检查ViewModelBase.IsInDesignMode

回答

2

正如在问题中提到,一个可能的方法来避免这个问题是检查我们是否处于设计模式首先在this gist完成:

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); 
      } 
     } 
    } 
}