MVVM Light Dispatcher helper design time error

642 views Asked by At

During construction of one of the View models for a Windows Phone 8.1 WinRT application, I have a call to DispatcherHelper.CheckBeginInvokeOnUI

I initialize DispatcherHelper during runtime at App.xaml.cs OnLauched event handler, but during design time this initialization is not done so when I call DispatcherHelper.CheckBeginInvokeOnUI, I get an Exception with the message "The DispatcherHelper is not initialized"

Is there any way to avoid this issue during design time other than calling DistpatcherHelper conditionally, checking ViewModelBase.IsInDesignMode first?

1

There are 1 answers

0
Camilo Martinez On BEST ANSWER

As mentioned in the question, one possible way to avoid this issue is checking whether we are in design mode first as done in 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);
            }
        }
    }
}