Search code examples
c#maui

.NET MAUI Navigation.PushAsync in Windows Issue


We have a simple .NET MAUI app with a Login page as the main page and after successful login, we move to (PushAsync) a menu page. This all works fine in Android but when we move over and begin testing in Windows we get an exception when we attempt to PushAsync the Menu page. The exception is 'Value does not fall within the expected range'. It appears to be in the Navigation area since we can create the page then add. Page creation works fine. Error happens only when we push it.

        //MenuPage mp = new MenuPage();
        //await Navigation.PushAsync(mp);

        await Navigation.PushAsync(new MenuPage());

Update: Menu page code below. The offender seems to be when I insert my Grid into Vertical stack layout. When I comment that line (vsl_menu_holder.Children.Insert(2, menu_grid)) out, the page loads (Ofcourse with no grid).

public MenuPage()
{
    InitializeComponent();
    Loaded += MenuPage_Loaded;

    Grid menu_grid = Globals.session.dcc_layouts.get_menu_grid(Globals.transactions_list, "User Menu");
    set_menu_click_event(menu_grid);
    vsl_menu_holder.Children.Insert(2, menu_grid);
}

Get menu grid loads a grid full of image buttons which again works fine in Android.

UPDATE:

I have done a little deeper dive into the menu makeup which is a grid full of image buttons and a caption label below the image button. Each grid item is a vertical stack layout with the button and the label. If I comment out the adding of the label to the vertical stack layout then it works fine??? Can you not do this in Windows?

    Grid g = new Grid();

    for (c = 0; c < rows; c++)
    {
        RowDefinition rd = new RowDefinition();
        rd.Height = GridLength.Auto;
        g.RowDefinitions.Add(rd);
    }

    for (c = 0; c < item_width; c++)
    {
        ColumnDefinition cd = new ColumnDefinition();
        cd.Width = GridLength.Star;
        g.ColumnDefinitions.Add(cd);
    }

    for (y = 0, num = 0; y < rows; y++)
    {

        for (x = 0; x < item_width; x++, num++)
        {

            ImageButton button_img = new ImageButton();
            button_img.Aspect = Aspect.AspectFit;
            button_img.StyleId = "Button" + y + x;
            button_img.BorderColor = Colors.Black;
            button_img.BorderWidth = 2;
            button_img.CornerRadius = 10;

            //MENU ICON
            button_img.Source = ImageSource.FromFile(Globals.session.icon_dir + "/device_icon_dcc_default_button.png");
            button_img.BackgroundColor = Colors.LightGray;
            button_img.Padding = 5;

            //MENU ITEM CAPTION
            Label label_button_title = new Label();
            label_button_title.HorizontalOptions = LayoutOptions.Center;
            label_button_title.VerticalOptions = LayoutOptions.Start;
            label_button_title.Text = "Button" + y + x;
            label_button_title.Padding = new Thickness(0, -5, 0, 0);

            VerticalStackLayout vsl = new VerticalStackLayout();

            vsl.Add(button_img);
            //vsl.Add(label_button_title);

            g.Add(vsl, x, y);
        }
    }

Thanks in advance


Solution

  • This line causes the issue,

    label_button_title.Padding = new Thickness(0, -5, 0, 0);
    

    You use the negative value -5, which crashes on Windows. It relates to the known issue on GitHub, Negative Padding values fail #6387. You may follow this issue up and get the latest updates.

    So far you may avoid using negative values for Windows platform. Conditional compilation may help you in some way,

    #if Android
           label_button_title.Padding = new Thickness(0, -5, 0, 0);
    #elif Windows
           label_button_title.Padding = new Thickness(0, 0, 0, 0);
    #endif
    

    Hope it helps!