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

Kid*_*ode 8 android android-activity xamarin xamarin.forms

我正在使用Xamarin.Forms创建一个跨平台应用程序,我所有的ContentPages都位于PCL.

我正在寻找一种方法来设置和锁定orientation单一的ContentPageLandscape,最好不必在每个特定平台的项目,以创建另一个活动.

由于我ContentPage.Content设置为a ScrollView,我已经尝试设置ScrollOrientationHorizontal,但是这不起作用.

我也尝试使用过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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

我担心这样做的唯一方法是创建一个专门Activity针对这个平台的第二个平台ContentPage,并将方向设置为横向.

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

目前正在Android中测试.

Dea*_*ane 14

讨厌这样说,但这只能使用自定义渲染器或特定于平台的代码来完成

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

在iOS中,您可以覆盖GetSupportedInterfaceOrientationsAppDelegate类返回一个UIInterfaceOrientationMask值时,Xamarin.Forms.Application.Current.MainPageContentPage你在intereted.



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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)