【问题标题】:MVVM Light Dispatcher helper design time errorMVVM Light Dispatcher helper 设计时错误
【发布时间】:2015-06-15 21:59:02
【问题描述】:

在为 Windows Phone 8.1 WinRT 应用程序构建视图模型之一期间,我致电 DispatcherHelper.CheckBeginInvokeOnUI

我在运行时在 App.xaml.cs OnLauched 事件处理程序中初始化 DispatcherHelper,但在设计时未完成此初始化,因此当我调用 DispatcherHelper.CheckBeginInvokeOnUI 时,我收到一条异常消息“DispatcherHelper 未初始化"

除了有条件地调用DistpatcherHelper,先检查ViewModelBase.IsInDesignMode,还有什么方法可以在设计时避免这个问题?

【问题讨论】:

    标签: c# winrt-xaml mvvm-light dispatcher


    【解决方案1】:

    如问题中所述,避免此问题的一种可能方法是首先检查我们是否处于设计模式,就像在 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);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多