如何为不同的方向切换不同的xaml

时间:2016-08-14 23:25:13

标签: xaml xamarin xamarin.forms orientation

我知道如何显示内容页面,但如何根据方向选择正确的布局。我想写xaml文件:一个用于垂直方向,另一个用于水平方向。是否可以在方向更改时切换xaml文件

1 个答案:

答案 0 :(得分:4)

  

Xamarin.Forms不提供任何原生事件来通知您的应用   共享代码中的方向更改。但是,SizeChanged事件   当页面的宽度或高度发生变化时,页面会触发。   当Page的宽度大于高度时,设备是   在横向模式中。

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
        }
    }
}

使用此功能,您可以通过编程方式将页面内容更改为方向上的新内容。

Content = // new content here.

对于某些患者,您可以尝试使用相同的XAML但仅更改某些视图的方向,例如here

来源here