【问题标题】:Xamarin Android, Xamarin Forms orientation changeXamarin Android,Xamarin Forms 方向更改
【发布时间】:2016-12-21 13:29:27
【问题描述】:

使用 Xamarin Forms UI 框架的 Xamarin Android 应用。 我检测到屏幕方向,并希望仅允许特定页面的纵向和横向,而仅允许所有其他页面的纵向。

我尝试调用 RequestOrientation 但这会强制方向保持不变并且不会再次触发我的方向更改通知。

【问题讨论】:

  • 您是否在 Android 解决方案属性中允许纵向以外的其他方向?

标签: xamarin.android xamarin.forms


【解决方案1】:

在您的 App.Xaml.cs 中有一个静态属性

public static bool IsPortraitOnly { get; set; }

在所有页面中将其设置为false,并在您希望仅纵向的页面中将其设置为true

在您的MainActivity android 项目中,覆盖方法OnConfigurationChanged

    public override void OnConfigurationChanged(Configuration newConfig)
    {
        //using to prevent the OnCreate from firing when rotating or screen size is changing
        if (App.IsPortraitOnly)
        {
            RequestedOrientation = ScreenOrientation.Portrait;
            newConfig.Orientation = Orientation.Portrait;
        }
        base.OnConfigurationChanged(newConfig);
    } 

以下是 android 中可用的方向值:

public enum Orientation
{
    Landscape = 2,
    Portrait = 1,
    Square = 3,
    Undefined = 0
}

【讨论】:

    【解决方案2】:

    确定您是处于纵向还是横向模式非常容易:

     static bool IsPortrait(Page p) { return p.Width < p.Height; }
    

    您可以使用 sizeChanged 事件。当用户从纵向模式变为横向模式时,SizeChanged 事件似乎只被调用一次

     SizeChanged += (sender, e) => Content = IsPortrait(this) ? portraitView : landscapeView;
    

    【讨论】:

      【解决方案3】:
      RequestedOrientation = ScreenOrientation.Portrait;
      

      如果你在代码端写代码,保持纵向模式。

      【讨论】:

        猜你喜欢
        • 2015-12-10
        • 2019-01-17
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 1970-01-01
        相关资源
        最近更新 更多