2016-07-26 168 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