hnabbasi hnabbasi - 2 months ago 25
C# Question

Prism for Xamarin.Forms unable to AutoWire

I am trying out Prism.Forms for my next client project. Unfortunately, I am seeing a strange behavior with my samples.

Pretext
It seems like XF or Prism does not like me including an existing XAML page (also mentioned by Brian Lagunas - The man - himself at the Evolve session). But renaming a page also throws it off and the ViewModelLocator can't seem to locate the ViewModel anymore. Even if I re-create the page with same name, It can't find it ViewModel. Manually setting the BindingContext works, but I am trying not to create my dependencies for constructor injection.

Question
While working on existing code, a minor change caused NullReference exception when using NavigationPage as a root. Here is an example of a working sample app

https://github.com/hnabbasi/xamarin/tree/master/XFPrism/XFPrism

I am using a NavigationPage and pushing a ContentPage. Then I push another ContentPage, then I show a Modal page via button click and communicate. I send parameters back to Second content page via NavigationParameters.

To break it, simply try to swap the ISayHello service with IPageDialogService. I am not sure what's going on that's breaking it.

Thanks in advance :)

Answer

As mentioned by Dan S. in the comments. The MainPage is not set when I was calling the PageDialogService to display the alert, hence the NullReference.

In case you really need to do something like this where you want to somehow wait for something to finish before doing an operation, try Task.Yield().

I fixed my scenario as below,

    // will yield for current operation of page appearing
    await Task.Yield(); 

    // by now, the page has appeared and set, so this should work fine
    SayHiCommand.Execute(this); 

I have used this workaround in the past. If you know a better way of handing this, please comment away.

Comments