2016-07-26 171 views
1

應用中有一些頁面,有些頁面是縱向的,但有些頁面是橫向的。 如果使用Android原生代碼,我可以做到這一點是這樣的:如何在xamarin.forms中旋轉頁面

<activity 
     android:name=".activity1" 
     android:screenOrientation="landscape" /> 
    <activity 
     android:name=".activity2" 
     android:screenOrientation="portrait"/> 

但如何界定xamarin.forms不同的屏幕方向

+1

結帳這樣的:http://stackoverflow.com/questions/29057972/xamarin-forms-on-ios-how設置屏幕方向爲頁面 –

+0

你是對的這樣做:)非常感謝你。 – Tom

回答

1

就可以實現這使得一個依賴

將界面在主窗體項目:

namespace Example 
{ 
    public interface IOrientationHandler 
    { 
     void ForceLandscape(); 
     void ForcePortrait(); 
    } 
} 

這樣稱呼它:

DependencyService.Get<IOrientationHandler>().ForceLandscape(); 
DependencyService.Get<IOrientationHandler>().ForcePortrait(); 

的Android

[assembly: Xamarin.Forms.Dependency(typeof(OrientationHandler))] 
namespace Example 
{ 
    public class OrientationHandler : IOrientationHandler 
    { 
     public void ForceLandscape() 
     { 
      ((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape; 
     } 

     public void ForcePortrait() 
     { 
      ((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait; 
     } 
    } 
} 

的iOS

[assembly: Xamarin.Forms.Dependency(typeof(OrientationHandlerImplementation))] 
namespace Example 
{ 
    public class OrientationHandlerImplementation : IOrientationHandler 
    { 
     public void ForceLandscape() 
     { 
      UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation")); 
     } 

     public void ForcePortrait() 
     { 
      UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation")); 
     } 
    } 
} 
+0

非常感謝。 – Tom

+0

@Tom歡迎您 –

+0

任何方法可以撤消此後?就像把它轉回到基於實際設備方向的設置方向? – hvaughan3