What I need to achieve is, Adding a Layout to the existing page on Changing the screen from Portrait to Landscape. I have managed to detect orientation change using void OnSizeAllocated(double width, double height) . But I cannot add a layout on this event.
My sample C# Code is
public class MyLayoutView : ContentPage
{
StackLayout portraitcontent = null;
StackLayout landscapecontent = null;
StackLayout footer = null;
public MyLayoutView ()
{
portraitcontent = new StackLayout ();
landscapecontent = new StackLayout ();
footer = new StackLayout ();
this.Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
this.BackgroundColor = Color.FromHex ("#ffffff");
this.Content = new StackLayout
{
Children =
{
portraitcontent ,
landscapecontent ,
footer
}
};
}
}
OnSizeAllocated Method
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (width < height) {
// In Portrait
portraitcontent .IsVisible = true;
landscapecontent.IsVisible = false;
Debug.WriteLine ("Application Is in portrait");
} else {
// In Landscape
portraitcontent .IsVisible = false;
landscapecontent.IsVisible = true;
landscapecontent.Children.Clear ();
Label LandscapeLabel= new Label
{
Text = "<Each Time Text Changes>",
HorizontalOptions = LayoutOptions.StartAndExpand,
TextColor=Xamarin.Forms.Color.FromHex("#000000")
};
landscapecontent.Children.Add(LandscapeLabel);
Debug.WriteLine ("Application Is in Landscape");
}
}
It throws cannot modify the collection while reenterancy is blocked error. I need to add a new label in stacklayout each time when orientation changes. How can I achieve it in a proper way??
Thanks in advance
To detect orientations without Xamarin. Essentials, monitor the SizeChanged event of the Page , which fires when either the width or height of the Page changes. When the width of the Page is greater than the height, the device is in landscape mode. For more information, see Display an Image based on Screen Orientation.
If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.
By default, Xamarin. Forms Layouts and Controls are designed to be responsive and will often grow and shrink to take advantage of the available screen space. If more control is needed, the Xamarin. Forms Page class has a SizeChanged event we can use to determine the available screen-size at runtime.
Xamarin. Essentials provides a single cross-platform API that works with any iOS, Android, or UWP application that can be accessed from shared code no matter how the user interface is created. See the platform & feature support guide for more information on supported operating systems.
On Size allocated is invoked multiple times when orientation change. I think you have to do more validations like checking the number of chidren to avoid duplication of views.
if(landscapecontent.Children.Count>1)
{
//Don't add any views
}
As long as I tried there is no event handler for rotation. Itz kind of hack in the code to change views when orientation changes.
Even though you can use the accepted answer here.. Hope it serves your purpose..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With