如何为不同的方向切换不同的 xaml
How to switch different xaml for different orientation
我知道如何显示内容页面,但我如何select 根据方向选择正确的布局。我想写 xaml 个文件:一个用于垂直方向,另一个用于水平方向。方向改变时是否可以切换 xaml 文件
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
}
}
}
有了这个,您可以根据方向以编程方式将页面内容更改为新内容。
Content = // new content here.
对于某些患者,您可以尝试使用相同的 XAML 但仅更改某些视图的方向,例如 here。
来源here.
我知道如何显示内容页面,但我如何select 根据方向选择正确的布局。我想写 xaml 个文件:一个用于垂直方向,另一个用于水平方向。方向改变时是否可以切换 xaml 文件
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
}
}
}
有了这个,您可以根据方向以编程方式将页面内容更改为新内容。
Content = // new content here.
对于某些患者,您可以尝试使用相同的 XAML 但仅更改某些视图的方向,例如 here。
来源here.