NullReferenceException when including Mvvm Light PCL in Windows Phone 7 app

Apr 4, 2013 at 10:18 AM
Hi,

I recently adapted my app to use the PCL-variant of MvvmLightToolkit. Everything works as expected, except for when I include the MvvmLight PCL into a WP7 app and run it on a WP7 emulator or device (Lumia 800). However there aren't any issues when this WP7 app is run on a WP8 emulator or device (HTC8X).

At startup, my VIewModelLocator complains about ViewModelBase.IsInDesignModeStatic:
  Message=NullReferenceException
  StackTrace:
       at GalaSoft.MvvmLight.ViewModelBase.IsInDesignModeNet()
       at GalaSoft.MvvmLight.ViewModelBase.IsInDesignModePortable()
       at GalaSoft.MvvmLight.ViewModelBase.get_IsInDesignModeStatic()
       at TestApp.ViewModelLocator.get_IsMocked()
       at TestApp.ViewModelLocator..cctor()
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
       at MS.Internal.TypeProxy.<>c__DisplayClass30.<GetCreateObjectDelegate>b__2a()
       at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
       at MS.Internal.XamlManagedRuntimeRPInvokes.CreateInstance(XamlTypeToken inXamlType, XamlQualifiedObject& newObject)
       at MS.Internal.XcpImports.Application_LoadComponentNative(IntPtr pContext, IntPtr pComponent, UInt32 cUriStringLength, String uriString, UInt32 cXamlStrLength, Byte* pXamlStr, UInt32 cAssemblyStrLength, String assemblyStr)
       at MS.Internal.XcpImports.Application_LoadComponent(IManagedPeerBase componentAsDO, String resourceLocator, UnmanagedMemoryStream stream, UInt32 numBytesToRead, String assemblyString)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at TestApp.App.InitializeComponent()
       at TestApp.App..ctor()
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
       at MS.Internal.TypeProxy.<>c__DisplayClass30.<GetCreateObjectDelegate>b__2a()
       at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
       at MS.Internal.FrameworkCallbacks.CreateKnownObject(IntPtr nativeRootPeer, UInt32 customTypeId, String initializationString, IntPtr& nativePeer, UInt32 isCreatedByParser)
       at MS.Internal.FrameworkCallbacks.CreateUnknownObject(String assemblyName, String typeName, IntPtr nativeRootPeer, String initializationString, UInt32& customTypeId, UInt32& coreTypeId, UInt32& typeFlags, IntPtr& nativePeer)
Furthermore, when my first RelayCommand gets instantiated, the following exception occurs:
System.IO.FileNotFoundException occurred
  Message=File or assembly name 'GalaSoft.MvvmLight.Platform.Net45, Version=4.1.27.18601, Culture=neutral', or one of its dependencies, was not found.
  StackTrace:
       at System.ThrowHelper.throwVersion37CompatException(ExceptionType newEType, String newString, ExceptionType oldEType, String oldString)
       at System.Reflection.Assembly.Load(String assemblyString)
       at GalaSoft.MvvmLight.Internal.ProbingAdapterResolver.ProbeForPlatformSpecificAssembly(String platformName)
       at GalaSoft.MvvmLight.Internal.ProbingAdapterResolver.ProbeForPlatformSpecificAssembly()
       at GalaSoft.MvvmLight.Internal.ProbingAdapterResolver.GetPlatformSpecificAssembly()
       at GalaSoft.MvvmLight.Internal.ProbingAdapterResolver.Resolve(Type type)
       at GalaSoft.MvvmLight.Internal.PlatformAdapter.Resolve[T](Boolean throwIfNotFound)
       at GalaSoft.MvvmLight.Command.RelayCommand..cctor()
       at TestApp.ViewModels.LoginViewModel.get_LoginCommand()
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[] index, StackCrawlMark& stackMark)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
       at System.Windows.CLRPropertyListener.get_Value()
       at System.Windows.PropertyAccessPathStep.ConnectToPropertyInSource(Boolean isSourceCollectionViewCurrentItem)
       at System.Windows.PropertyAccessPathStep.ConnectToProperty()
       at System.Windows.PropertyAccessPathStep.ReConnect(Object newSource)
       at System.Windows.PropertyPathListener.ReConnect(Object source)
       at System.Windows.Data.BindingExpression.SourceAcquired()
       at System.Windows.Data.BindingExpression.InheritanceContextChanged(Object sender, EventArgs e)
       at System.Windows.DependencyObject.OnInheritanceContextChanged(Object sender, EventArgs e)
       at System.Windows.DOCollection.AddInternal(DependencyObject value)
       at System.Windows.PresentationFrameworkCollection`1.Add(DependencyObject value)
       at System.Windows.DependencyObjectCollection`1.System.Collections.IList.Add(Object value)
       at MS.Internal.XamlManagedRuntimeRPInvokes.Add(XamlQualifiedObject& qoCollection, XamlPropertyToken inCollectionProperty, XamlQualifiedObject& inValue)
       at MS.Internal.XcpImports.Application_LoadComponentNative(IntPtr pContext, IntPtr pComponent, UInt32 cUriStringLength, String uriString, UInt32 cXamlStrLength, Byte* pXamlStr, UInt32 cAssemblyStrLength, String assemblyStr)
       at MS.Internal.XcpImports.Application_LoadComponent(IManagedPeerBase componentAsDO, String resourceLocator, UnmanagedMemoryStream stream, UInt32 numBytesToRead, String assemblyString)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at TestApp.View.LoginPage.InitializeComponent()
       at TestApp.View.LoginPage..ctor()
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Activator.InternalCreateInstance(Type type, Boolean nonPublic, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type)
       at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at System.Delegate.DynamicInvokeOne(Object[] args)
       at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
       at System.Delegate.DynamicInvoke(Object[] args)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
       at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
       at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
       at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
       at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
The previous iteration of this app used the normal version of MvvmLight without issues.

Thanks
Apr 8, 2013 at 9:26 AM
Okay, I found it, and it's totally on my end. Somehow, I had my debugger set to break on "First chance exceptions" as described here http://social.msdn.microsoft.com/Forums/en-US/vsdebug/thread/f3e0f648-f104-4c46-97b0-db7e46edf3c2/. I probably did this myself while attempting to debug design time data in Blend. Sorry if I wasted anyone's time! Good to know, though.


Kind regards,
Alex