My UI did not response for orientation change

lak1104lak1104 Member ✭✭
edited March 13 in Xamarin.Forms

I am developing a native mobile app for all platforms. I have created my own theme content page. Then after deployment on android when I make phone landscape it did not respond. what's the reason here.

Here is my base content page.

                   public abstract class BaseContentPage : ContentPage
        {
public readonly BaseViewModel BaseViewModel;

protected bool _isNavigated = false;

public BaseContentPage(BaseViewModel baseViewModel)
{
    BaseViewModel = baseViewModel;
}

public abstract void Navigate(SelectedItemChangedEventArgs e);
protected abstract override void OnAppearing();
protected override void OnDisappearing()
{
    _isNavigated = true;
}

public BaseContentPage()
{

}
 }

here Xaml

    <views:BaseContentPage
           xmlns:views="clr-namespace:DipsDemoXaml.Views"

        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
        xmlns:Resource="clr-namespace:DipsDemoXaml.Resources"

        x:Class="DipsDemoXaml.Views.WardListPage" Title="{x:Static Resource:AppResources.WardListPageTitle}">
                  <StackLayout BackgroundColor="{StaticResource DefaultBackgroundColor}" Orientation="Vertical" x:Name="s1">

I even try this also in code behind constructor I call size changed and create a method called Wardpagesizechanged.

    public WardListPage(WardListPageViewModel wardListViewModel) : base(wardListViewModel)
{
    InitializeComponent();
    this.SizeChanged += wardpagesizechanged;


}
         Wardpagesizechanged method

  private void wardpagesizechanged(object sender, EventArgs e)
{
   if(this.Width> this.Height)
    {
        s1.Orientation = StackOrientation.Horizontal;
    }
    else
    {
        s1.Orientation = StackOrientation.Vertical;
    }
   }

what is the problem here, I am clueless

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Try to use OnSizeAllocated instead.
    Note that the OnSizeAllocated method may be called many times when a device is rotated.

    Refer the solution for reacting to Changes in Orientation (StackLayout)

    protected override void OnSizeAllocated (double width, double height){
        base.OnSizeAllocated (width, height);
        if (width != this.width || height != this.height) {
            this.width = width;
            this.height = height;
            if (width > height) {
                outerStack.Orientation = StackOrientation.Horizontal;
            } else {
                outerStack.Orientation = StackOrientation.Vertical;
            }
        }
    }
    

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=windows#stacklayout

Sign In or Register to comment.