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

标签 xaml xamarin xamarin.forms orientation

我知道如何显示内容页面,但如何根据方向选择正确的布局。我想编写 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,但仅更改某些 View 的方向,例如 here .

来源 here .

关于xaml - 如何为不同的方向切换不同的 xaml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947505/

相关文章:

.net - 在哪里可以找到一些 WPF 动画示例?

c# - DataGrid 的自定义列模板

android - Xamarin 按钮边框问题

sqlite - Xamarin.Forms SQLite CreateTableAsync()返回类型的用法

android - Autofac、Android 和 Xamarin Forms == 未找到可访问的构造函数

android - Xamarin 上出现过时的 'Android SDK Build-tools' 错误

.net - 如何确定 DockPanel 中包含的所有项目的高度

xamarin - NetStandard 库未获得对 VSTS 构建代理的依赖关系

listview - 如何在Xamarin表单中设计listview viewcell设计?

azure - 用于 Azure AD B2C 和 Xamarin 的 MSAL 或 ADAL 库