Search code examples
c#xamlxamarin.formspopupxamarin-community-toolkit

Shell.Current.GoToAsync to xct:Popup results in error System.NullReferenceException


I would like to display a Popup with the Xamarin Community Toolkit Popup, but I try to navigate to it using Shell I receive the error "System.NullReferenceException Message=Object reference not set to an instance of an object."

The xaml for my popup:

<?xml version="1.0" encoding="utf-8" ?>
<xct:Popup 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
    x:Class="Restuco_Tables.Views.AddAreaPopup"
    Shell.PresentationMode="ModalAnimated"
    Size="300, 300">
    <StackLayout>
        <Label Text="This is a Popup!" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/>
    </StackLayout>
</xct:Popup>  

The codebehind:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AddAreaPopup : Popup
{
    public AddAreaPopup()
    {
        InitializeComponent();
    }
}

I have registered Routing in AppShell:

Routing.RegisterRoute(nameof(AddAreaPopup), typeof(AddAreaPopup));

My call to the Popup using Shell:

private async void tbiNewArea_Clicked(object sender, EventArgs e)
{
    await Shell.Current.GoToAsync("/AddAreaPopup");
}

Solution

  • From Shell Navigation, we can see that Shell.Current.GoToAsync need to ShellNavigationState type data, apparently popup page is not, so you have this error message.

    If you use one simple ContentPage, having no problem.

    So I suggest you can use Rg.Plugins.Popup Popup Pages, then using await PopupNavigation.Instance.PushAsync(new Page2()); to go to PopupPage from one ContentPage in Shell.

    private async void btn1_Clicked(object sender, EventArgs e)
    {
        await PopupNavigation.Instance.PushAsync(new Page2());
        //await Shell.Current.GoToAsync("page2");
           
    }
    

    Finally, to using await PopupNavigation.Instance.PopAsync(true); to close this popupPage.

    private async void btnclose_Clicked(object sender, EventArgs e)
    {
        await PopupNavigation.Instance.PopAsync(true);
    }
    

    Have xaml like this:

    <popup:PopupPage
        x:Class="shellsample.Views.Page2"
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
    
        <StackLayout>
            <Frame
                BackgroundColor="Red"
                BorderColor="Black"
                CornerRadius="30">
                <StackLayout>
                    <Label
                        HorizontalOptions="CenterAndExpand"
                        Text="Welcome to Xamarin.Forms!"
                        VerticalOptions="CenterAndExpand" />
                    <Button
                        x:Name="btnclose"
                        Clicked="btnclose_Clicked"
                        Text="close" />
                </StackLayout>
            </Frame>
        </StackLayout>
    </popup:PopupPage>
    

    You will also need to init Rg.Plugins.Popup.Popup in MainActivity.cs OnCreate method.

    Rg.Plugins.Popup.Popup.Init(this);
    

    You can also submit one feature in shell navigation on Add support to Shell Navigation