Search code examples
c#xamarinxamarin.formsuwpaspnetboilerplate

Xamarin.UWP Unhandled Exception during launch


My objective is to extend an existing Xamarin project with UWP.

I have two shared projects , X.mobile.shared and X.application.shared , so i added reference for the Uwp project to this two project.

After following the steps to add uwp from the documentation, I have also installed all the Nuget packages available in these shared projects as it's noted in the answer of these issue .

Now when I debug the UWP project, I don't get any error but an unhandled exception, I tried to pin point the ligne that trigger that exception and I found that uncommenting this line:

 rootFrame.Navigate(typeof(MainPage), e.Arguments);

Triggers the unhandled exception:

unhandled exception

The message contains,as you see in the image:

    Message "Object reference not set to an instance of an object." 

which means that there is a referencing to a null object !

What am I doing wrong ?

Update

With a BreakPoint on the ligne triggering the error , i tried to see the value of the method is arguments , i found that DeclaringMethod called by typeofMain() is returning "SystemInvalidOpertaionException". SystemInvalidOperationException

what that means ?

Update 2

Getting the advice of @Martin Zikmund , i checked the Stack trace and i found that the problem occurs at :

  Acme.PhoneBookDemo.Core.Dependency.DependencyResolver.Resolve[T]()\r\n at Acme.PhoneBookDemo.App.OnStart()

back to that class, this is the code of OnStart() method:

 protected override async void OnStart()
    {
        base.OnStart();

        if (Device.RuntimePlatform == Device.iOS)
        {
            SetInitialScreenForIos();
            await UserConfigurationManager.GetIfNeedsAsync();
        }

        await DependencyResolver.Resolve<INavigationService>().InitializeAsync();

        OnResume();
    }

the problem is triggred when the debugger arrive at this line:

 await DependencyResolver.Resolve<INavigationService>().InitializeAsync();

Solution

  • I got a response from xamarin-docs 's github , telling that in order to extend your existing UWP project with UWP all the nuget packages you use in your solution should have a support to UWP (not only the ones that exists in .shared).

    In my solution i have many packages that dosen't support UWP, example :

    ACRSupport supports only android and ios

    In conclusion, aspnetzero 's project can't be extended to use UWP.

    UPDATE OF ANSWER:

    A co-worker in my organisation found a solution by coping the implementation of *.droid project.

    This answer is useful for aspBoilerplate user, in particular aspnetzero user, who want to extend his project with UWP.

    This are the steps , to make it work :

    1. First ,follow this tutorial
    2. If you run you project after the first step , you may be surprised by an "unhandled exception=Object reference not set to an instance of an object.", this happens because the Dependancy Injection Container(aka service locator) can't find the registration of your uwp abpmodule, So :

      2.1. Define a Module for your add Uwp project (Add>NewItem>PhoneBookDemoXamarinUwpModule.cs):

       using Abp.Modules;
       using Abp.Reflection.Extensions;
      
         namespace Acme.PhoneBookDemo
         {
             [DependsOn(typeof(PhoneBookDemoXamarinSharedModule))]
                public class PhoneBookDemoXamarinUwpModule : AbpModule
                {
                  public override void Initialize()
                  {     IocManager.RegisterAssemblyByConvention(typeof(PhoneBookDemoXamarinUwpModule).GetAssembly());
                  }
              }
          }
      

      In the initialize method above we register dependencies , IocManager is used to register the module class.

    3. in App.xaml.cs 's OnLaunched() method , add these lines :

      //rootFrame.NavigationFailed += OnNavigationFailed;
      
        ApplicationBootstrapper.InitializeIfNeeds<PhoneBookDemoXamarinUwpModule>();
            await StartUpApplicationAsync();
      
       //Xamarin.Forms.Forms.Init(e); // requires the `e` parameter
      

      then add this methods:

       private async Task StartUpApplicationAsync()
       {
          await UserConfigurationManager.GetIfNeedsAsync();
        }
      
      private static void ConfigureFlurlHttp()
        {
          FlurlHttp.Configure(c =>
           {
              c.HttpClientFactory = new ModernHttpClientFactory();
           });
      
        }
      
    4. Add these Nuget Packages to your Uwp Project:

      Xamarin.forms (ensure it's the same version with your *.Mobile.shared project)

      Win2D.uwp

    5. Finally from your uwp project, add a reference to Acme.PhoneBook.Mobile.shared , Acme.PhoneBook.Application.shared & Acme.PhoneBook.Application.Core

    And that is it , it would run for you , as it did to me