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

时间:2015-08-22 15:05:52

标签: android android-activity xamarin xamarin.forms

我使用Xamarin.Forms创建跨平台应用程序,我的所有ContentPages都位于PCL内。

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

由于我的Landscape设置为ContentPage.Content,我们已尝试将ScrollView设置为ScrollOrientation,但这不起作用。

我还尝试使用Horizontal,但我无法在此处看到RelativeLayout属性。

Orientation

我尝试的最后一件事是使用Xamarin Studio的Intellisense版本和Xamarin Forms API Doc's来查看我可用的不同布局,其中没有一个具有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; } } 属性。

我担心唯一的方法是为这个Orientation创建第二个特定Activity平台,并将方向设置为横向。

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

目前正在Android中测试。

4 个答案:

答案 0 :(得分:14)

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

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

在iOS中,您可以覆盖GetSupportedInterfaceOrientations课程中的AppDelegate,以便在UIInterfaceOrientationMask Xamarin.Forms.Application.Current.MainPage成为ContentPage时返回其中一个[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; } } } 值英寸



<强>的Android

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;
    }
}

<强>的iOS

{
  "members": [
    {"name": "John", "location": "QC", "age": 25},
    {"name": "Jesse", "location": "PQ", "age": 24},
    {"name": "Jez", "location": "PQ", "age": 23},
    {"name": "Ry", "location": "PQ", "age": 22},
    {"name": "Barry", "location": "PQ", "age": 25},
    {"name": "Rikki", "location": "PQ", "age": 35},
    {"name": "Ross", "location": "PQ", "age": 33},
    {"name": "Chiz", "location": "PQ", "age": 25},
    {"name": "Gel", "location": "PQ", "age": 24},
    {"name": "Cherry", "location": "PQ", "age": 22}
  ]
}

答案 1 :(得分:1)

这也可以通过使用MessagingCenter类将表单项目中的消息发送到主机项目来完成。不使用自定义渲染器或依赖服务,如下所示,

public partial class ThirdPage : ContentPage
  {
      protected override void OnAppearing()
      { 
          base.OnAppearing(); 
         MessagingCenter.Send(this, "allowLandScapePortrait");
      }
       //during page close setting back to portrait
      protected override void OnDisappearing()
      {
          base.OnDisappearing(); 
          MessagingCenter.Send(this, "preventLandScape");
      }
  }

更改mainactivity以接收消息并设置RequestedOrientation

[Activity(Label = "Main", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,ScreenOrientation = ScreenOrientation.Portrait)]
 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
  {        
  //allowing the device to change the screen orientation based on the rotation
  MessagingCenter.Subscribe<ThirdPage>(this, "allowLandScapePortrait", sender =>
  { 
   RequestedOrientation = ScreenOrientation.Unspecified;
  });
 //during page close setting back to portrait
 MessagingCenter.Subscribe<ThirdPage>(this, "preventLandScape", sender =>
  { 
   RequestedOrientation = ScreenOrientation.Portrait;
  });
 }

在我的博文中查看更多内容:http://www.appliedcodelog.com/2017/05/force-landscape-or-portrait-for-single.html

答案 2 :(得分:1)

如果您在Android上也遇到问题,设备旋转使您返回提示用户输入电子邮件,则可以在此处跟踪ADAL和MSAL的修复进度:

https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/1622 https://github.com/xamarin/xamarin-android/issues/3326

答案 3 :(得分:0)

Dealdiane的代码对我来说效果很好,只有很小的改变:

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

    IRotationLock page = Element as IRotationLock;
    if ( page == null )
        return;

    var activity = (Activity) Context;

    if ( visibility == ViewStates.Gone ) {
        // Revert to previous orientation
        activity.RequestedOrientation = _previousOrientation;
    } else if ( visibility == ViewStates.Visible ) {
        if ( _previousOrientation == ScreenOrientation.Unspecified ) {
            _previousOrientation = activity.RequestedOrientation;
        }

        switch ( page.AllowRotation() ) {
            case RotationLock.Landscape:
                activity.RequestedOrientation = ScreenOrientation.SensorLandscape;
                break;
            case RotationLock.Portrait:
                activity.RequestedOrientation = ScreenOrientation.SensorPortrait;
                break;
        }
    }
}