就可以实现这使得一个依赖
将界面在主窗体项目:
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"));
}
}
}
结帐这样的:http://stackoverflow.com/questions/29057972/xamarin-forms-on-ios-how设置屏幕方向为页面 –
你是对的这样做:)非常感谢你。 – Tom