LittleFunny
LittleFunny

Reputation: 8385

How to switch different xaml for different orientation

I know how to show a content page but how do I select the right layout based on the orientation. I wanted to write xaml files: one for vertical orientation and other one for horizontal orientation. Is it possible to switch the xaml file when orientation changes

Upvotes: 4

Views: 2389

Answers (1)

jzeferino
jzeferino

Reputation: 7850

Xamarin.Forms does not offer any native events for notifying your app of orientation changes in shared code. However, the SizeChanged event of the Page 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.

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) {
            // landscape
        } else {
            // portrait
        }
    }
}

With this you can change programmatically the content of your page to a new content base on the orientation.

Content = // new content here.

With some patient you could try to use the same XAML but only changing the orientation of some views, example here.

Source here.

Upvotes: 5

Related Questions