Adding a UWP project into an XF app results in an exception

Will.ItraxWill.Itrax CAMember ✭✭✭

Hi,
I have an existing XF app that works in Droid and iOS. My task is to add in a UWP project. To do that I followed the instructions in:
https://developer.xamarin.com/guides/xamarin-forms/platform-features/windows/installation/universal/

I also compared my code with other examples of UWP in XF.

The UWP MainPage throws an exception that I can't figure out. Here is a bit of my code:

    public MainPage()
    {
        try
        {
            this.InitializeComponent();

            // add this line
            LoadApplication(new MyAppName.App());
        }
        catch (Exception ex)
        {
            string msg = string.Format("Exception in MainPage:constructor: {0} ", ex.ToString());
            Debug.WriteLine(msg);
        }
    }

And the exception:
` Exception thrown: 'System.ArgumentNullException' in Xamarin.Forms.Platform.UAP.dll

Exception in MainPage:constructor: System.ArgumentNullException: Value cannot be null.

Parameter name: newRoot

   at Xamarin.Forms.Platform.UWP.Platform.SetPage(Page newRoot)

   at Xamarin.Forms.Platform.UWP.WindowsBasePage.LoadApplication(Application application)

   at i_TraxMobile.UWP.MainPage..ctor() 

`
The exception happens on the LoadApplication line.
Any insight or ideas are much appreciated.
Regards,
Will

Answers

  • Will.ItraxWill.Itrax CAMember ✭✭✭

    Found it.
    For various reasons, initially, I had
    MainPage = new MainPage();
    in the PCL App.xaml.cs OnStart method instead of the constructor. It worked for Droid and iOS. UWP does things differently and it was giving me a Null exception.
    Moving it back to the constructor gets me past the exception.

  • DamianSuessDamianSuess USMember ✭✭
    edited March 22

    And if you're using Prism:
    When switching from regular ContentPage to a MasterDetailPage, don't forget to change the NavigationService URI in OnInitialized() inside your App.xaml.cs file.

    Old

        protected override async void OnInitialized()
        {
          InitializeComponent();
    
          await NavigationService.NavigateAsync("NavigationPage/MainView");
        }
    

    New

    Note, MainView is our MasterDetailPage, and WelcomeView is our Detail landing page.

        protected override async void OnInitialized()
        {
          InitializeComponent();
    
          await NavigationService.NavigateAsync("MainView/NavigationPage/WelcomeView");
        }
    
Sign In or Register to comment.