.Net Maui Content Page vs Xamarin Content Page

331 views Asked by At

I am converting a Shell Xamarin app to a Shell .Net Maui. In my Xamarin project, one of my detail pages (called from the MainPage) is a Xmal ContentPage with all (100%) of the Content built in the code behind. I have a BuildForm() method called in the OnAppearing() override method that builds the form and at the end of it put all of my controls into a Stacklayout and set the contents to that stacklayout.

            StackLayout stackLayout = new StackLayout
        {
            Orientation = StackOrientation.Vertical,
            Children = { connectionView, scrollView, slbuttons, slbuttons1, btnShareAuthority },
            Padding = new Thickness(0, 0, 0, 20),
        };
        Content = stackLayout;

This detail page has a shared sub page to list relevant messages. This message page can be called by any one of the detail pages to show messages relevant to that detail. When navigating back, using the apps back button from this messages page the OnAppearing is called and then the BuildForm() method. The page is rebuilt and the page show properly. Now for the problem. In my Maui app I attempt to do the exact same thing. Navigating from the MainPage to the detail page all works fine. However when navigating back from the messages page (device back button) the app seems to just die on the Content = stackLayout; line. (As a side note stacklayout in the Maui app is really a grid as Maui needs the scrollview to be in a grid.) I do not get any error messages or unhandled exception, it just stops and the app is left on the messages page. I then can tap the back button again and it will navigate to the details page but this time it will be a blank page. I have come up with a work around and that is to determine if the page Content is null. So in my OnAppearing() method I do

    if(Content == null)
    {
        BuildForm();
    }

This will be called both on the initial load from the Mainpage and navigating back from the Messages page. In the BuildForm() method, which will be called only on the initial load of the form, I test it also:

        if(Content == null)
        Content = stacklayout;

So as it appears the Content in a page's life cycle (at least in my case) can only be set once. I even tried to set the Content to null then set it to the newly build grid, but that won't work either. Luckily for me the Detail page data is static throughout this navigation. So the big question: Is this intended? I searched Maui Documentation and found nothing helpful. Why does it work in Xamarin but not in Maui. Thanks in advance for your input.

1

There are 1 answers

1
Gary H On

As it turns out there is a difference between Xamarin and Maui in this respect. @ToolmakeSteve asked about the Children which caused me to make sure all children are instantiated in the OnAppearing method. The custom control connectionView was not. After re-instantiating it in the OnAppearing all worked as expected. The OnAppearing now looks like this:

protected override void OnAppearing()
{
    base.OnAppearing();
    connectionView = new ConnectionView();
    WarrantInfo = new Warrant();
    WarrantInfo = viewModel.WarrantInfo;
    BuildForm();
}