0
votes

I am trying to use Xamarin Forms Previewer in my Project with MVVM Prism and Unity Container and I am getting this error:

enter image description here

Here is the extracted stack for reference:

Xamarin.VisualStudio.ActivationManager|Error|0| The renderer encountered an error while rendering this file: System.ArgumentNullException: Value cannot be null. Parameter name: instance

at Microsoft.Practices.Unity.UnityContainer.RegisterInstance (System.Type t, System.String name, System.Object instance, Microsoft.Practices.Unity.LifetimeManager lifetime) in :0

at Microsoft.Practices.Unity.UnityContainerExtensions.RegisterInstance[TInterface] (Microsoft.Practices.Unity.IUnityContainer container, TInterface instance) in :0

at SellerNS.Bootstrapper.RegisterTypes () in C:\Users\myrepo\Bootstrapper.cs:48

at Prism.Unity.UnityBootstrapper.Run () in :0

at Prism.Bootstrapper.Run (Xamarin.Forms.Application app) in :0

at SellerNS.App..ctor () in C:\Users\myrepo\Seller\App.xaml.cs:20

at at offset 12 in file:line:column :0:0

at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661

I am using:

  • Visual Studio 2015 - 14.0.25424.00
  • Update 3 JDK 1.8.0_121
  • Xamarin 4.3.0.789-cycle9
  • Prism.Forms 6.0.1
  • Prism.Unity 6.1.1
  • Unity 4.0.1

Any idea how to fix it?

1
Not the type of help you were expecting but... does your previewer work without Unity (or this Prisms components) ? From my experience getting it to work is not a simple task. The problem might not be Unity related. I suggest creating a Hello World app and checking if the previewer runs with your current sdk/xamarin dlls to check this out from the list. - Veverke
@Veverke yes, the previewer is working fine on a Hello World project. - danieljaguiar
Tempted to close as dupe of the canonical NRE question. Your problem is that you are doing work in a constructor. It's being called at design time, but because you're not hosted in your .exe, something you are trying to use is null. You need to 1) examine all types potentially loaded in the designer 2) examine their constructors 3) perform null checking, try/catch and throw a wrapped exception, or check to see if you're in design mode (unsure w/xamarin how to do that) prior to doing any work. Also, don't do work in your constructors. - user1228
I am guessing it is a bug in the Xamarin Previewer. The Xamarin Previewer should be just a XAML reader and renderer - danieljaguiar
Let me add to above... The App constructor isn't changed from the template. I have no idea why the Previewer is going to App Constructor to render a XAML that has nothing to do with that. I bet if you build out from Prism Template, you get the same error. Since I know the Prism.Forms is somewhat being used in Xamarin, and StackOverflow is being leveraged to get some help, I hope I can find some folks who already saw that and could fix it. But I don't think I fall into NRE because I can't handle it on the closed black box Xamarin Previewer and I am on default. - danieljaguiar

1 Answers

0
votes

I was getting a similar error with the Xamarin Forms Previewer when using Prism and it was resolved by adding a empty default constructor in App.xaml.cs as indicated here: https://github.com/PrismLibrary/Prism/issues/1037

So adding public App() : this(null) { } to App.xaml.cs worked for me.

Also, once I made the suggested change above, I had to clean the whole solution, rebuild it and then restart Visual Studio before launching the Xamarin.Forms Previewer again to get it all working as required.