android – 如何在Xamarin.Forms中设置ContentPage方向

前端之家收集整理的这篇文章主要介绍了android – 如何在Xamarin.Forms中设置ContentPage方向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Xamarin.Forms创建跨平台应用程序,我的所有ContentPages都位于PCL中.

我正在寻找一种方法来设置和锁定单个ContentPage到Landscape的方向,最好不必在每个特定于平台的项目中创建另一个活动.

由于我的ContentPage.Content设置为ScrollView,我尝试将ScrollOrientation设置为Horizo​​ntal,但是这不起作用.

我也尝试过使用RelativeLayout,但我在这上面看不到Orientation属性.

public class PlanningBoardView : ContentPage //Container Class.
    {
        public PlanningBoardView()
        {
            scroller = new ScrollView ();

            Board = new PlanningBoard();

            scroller.Orientation = ScrollOrientation.Horizontal;
            scroller.WidthRequest = Board.BoardWidth;
            scroller.Content = Board;

            Content = scroller;
        }
    }

我尝试的最后一件事是使用Xamarin Studio的Intellisense版本和Xamarin Forms API Doc’s来查看我可用的不同布局,其中没有一个具有Orientation属性.

我担心唯一的方法是为这个ContentPage创建第二个特定于平台的Activity,并将方向设置为landscape.

虽然这种方法可行,但它使屏幕之间的导航变得更加复杂.

目前正在Android中测试.

解决方法

讨厌这样说,但这只能使用 custom renderers或特定于平台的代码来完成

在android中,您可以将MainActivity的RequestedOrientation属性设置为ScreenOrientation.Landscape.

在iOS中,当Xamarin.Forms.Application.Current.MainPage是您所在的ContentPage时,您可以覆盖AppDelegate类中的GetSupportedInterfaceOrientations以返回其中一个UIInterfaceOrientationMask值.

Android的

[assembly: Xamarin.Forms.ExportRenderer(typeof(MyCustomContentPage),typeof(CustomContentPageRenderer))]

public class CustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
    private ScreenOrientation _prevIoUsOrientation = ScreenOrientation.Unspecified;

    protected override void OnWindowVisibilityChanged(ViewStates visibility)
    {
        base.OnWindowVisibilityChanged(visibility);

        var activity = (Activity)Context;

        if (visibility == ViewStates.Gone)
        {
            // Revert to prevIoUs orientation
            activity.RequestedOrientation = _prevIoUsOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _prevIoUsOrientation;
        }
        else if (visibility == ViewStates.Visible)
        {
            if (_prevIoUsOrientation == ScreenOrientation.Unspecified)
            {
                _prevIoUsOrientation = activity.RequestedOrientation;
            }

            activity.RequestedOrientation = ScreenOrientation.Landscape;
        }
    }
}

iOS版

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application,UIWindow forWindow)
    {
        if (Xamarin.Forms.Application.Current == null || Xamarin.Forms.Application.Current.MainPage == null)
        {
            return UIInterfaceOrientationMask.Portrait;
        }

        var mainPage = Xamarin.Forms.Application.Current.MainPage;

        if (mainPage is MyCustomContentPage ||
           (mainPage is NavigationPage && ((NavigationPage)mainPage).CurrentPage is MyCustomContentPage) ||
           (mainPage.Navigation != null && mainPage.Navigation.ModalStack.LastOrDefault() is MyCustomContentPage))
        {
            return UIInterfaceOrientationMask.Landscape;
        }

        return UIInterfaceOrientationMask.Portrait;
    }
}
原文链接:https://www.f2er.com/android/316620.html

猜你在找的Android相关文章