2015-08-22 116 views
6

我使用Xamarin.Forms創建跨平臺應用程序,我所有的ContentPages都位於PCL之內。如何在Xamarin.Forms中設置ContentPage方向

我正在尋找一種方法來設置和鎖定一個ContentPageLandscape,的orientation最好,而不必在每個特定平臺的項目,以創建另一個活動。

由於我的ContentPage.Content設置爲ScrollView,因此我嘗試將ScrollOrientation設置爲Horizontal,但這種方式無效。

我也試過使用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的版本智能感知和Xamarin Forms API Doc's通過提供給我不同的佈局看過去的事情,沒有一個有Orientation財產。

我擔心要做到這一點的唯一方法是創建第二個特定平臺Activity,僅用於這一個ContentPage並將方向設置爲橫向。

雖然這種方法可行,但它使屏幕之間的導航更加複雜。

這是目前正在Android中測試。

回答

12

討厭這樣說,但是這隻能使用custom renderers或平臺特定的代碼

在Android中,您可以在MainActivity的RequestedOrientation屬性設置爲ScreenOrientation.Landscape完成。

在iOS中,您可以在AppDelegate類中重寫GetSupportedInterfaceOrientations返回UIInterfaceOrientationMask值之一,當Xamarin.Forms.Application.Current.MainPage是你在intereted的ContentPage



的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; 
    } 
} 
+0

的任何機會一個例子? (Android會的。) – KidCode

+0

當然可以。我編輯了我的答案。該示例將強制橫向模式,但您可以將其更改爲適合您的需求。 – Dealdiane

+0

我已經實現了iOS版本,但它不會自動旋轉...是可能的嗎? – GBreen12

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

這也可以從表項目傳送郵件MessagingCenter類主體項目完成。不使用自定義呈現或依賴服務如下,mainactivity

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

變化在我的博客文章,以獲得更多的信息,並設置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

相關問題