ORIENTATION BEHAVIOR

Giorgio Sfiligoi 391 Reputation points
2025-04-26T09:55:57.6733333+00:00

Consider the following simple test program, in NET MAUI 8.0:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ListOrientation.View.ListPage"
             Title="ListPage">

    <CollectionView ItemsSource="{Binding Items}" SelectionMode="None">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Label Text="{Binding .}" HorizontalOptions="Fill" BackgroundColor="Yellow"/>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

using ListOrientation.ViewModel;

namespace ListOrientation.View;

public partial class ListPage : ContentPage
{
	public ListPage(ListViewModel viewModel)
	{
		InitializeComponent();
		BindingContext = viewModel;
	}
}

using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace ListOrientation.ViewModel;

public partial class ListViewModel : ObservableObject
{
    public ObservableCollection<string> Items { get; set; } = new();

    public ListViewModel()
    {
        PopulateList();
    }

    private void PopulateList()
    {
        Items.Clear();
        for (int i = 0; i < 50; i++)
            Items.Add($"Item {i.ToString()}");
    }
}

If ListPage is the main page of the program, it runs OK.

But in my test ListPage is called from the MainPage by means of a button:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ListOrientation.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Label
                Text="Hello, World!"
                Style="{StaticResource Headline}"
                SemanticProperties.HeadingLevel="Level1" />

            <Label
                Text="Welcome to &#10;.NET Multi-platform App UI"
                Style="{StaticResource SubHeadline}"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

            <Button Text="Go to list" Clicked="Button_Clicked"/>
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

In this case, repeatedly changing the orientation, at times the width of the list does not fill the screen. This seems to happen randomly.

I was thinking to add the following in ListPage:

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
		
		//  TODO: force redraw
    }

But I could not figure out how to force redraw.

Can anybody help?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,084 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.