【发布时间】:2018-07-10 14:46:12
【问题描述】:
我需要在不旋转设备的情况下更改 Xamarin.Forms 中视图的方向,以便视图应该像在横向模式下一样布局。
有什么方法可以实现吗?在 Xamarin.Forms 中还是在 Xamarin.iOS 中?
【问题讨论】:
标签: xamarin xamarin.forms xamarin.ios
我需要在不旋转设备的情况下更改 Xamarin.Forms 中视图的方向,以便视图应该像在横向模式下一样布局。
有什么方法可以实现吗?在 Xamarin.Forms 中还是在 Xamarin.iOS 中?
【问题讨论】:
标签: xamarin xamarin.forms xamarin.ios
对于依赖服务中的iOS,编写以下方法
public void ChangeLandscapeOrientation()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
UINavigationController.AttemptRotationToDeviceOrientation();
}
在需要时调用 ChangeLandscapeOrientation 方法。
在 AppDelegate.cs
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{
var mainPage = Xamarin.Forms.Application.Current.MainPage;
if (mainPage is YourPage || (mainPage is NavigationPage &&
((NavigationPage)mainPage).CurrentPage is YourPage) || (mainPage.Navigation != null &&
mainPage.Navigation.ModalStack.LastOrDefault() is YourPage))
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
希望对你有帮助!
【讨论】: