我正在使用 Xamarin.Forms 创建一个跨平台应用程序,我的所有ContentPages
位于PCL
.
我正在寻找一种方法来设置和锁定orientation
单个的ContentPage
to Landscape
,最好不必在每个特定于平台的项目中创建另一个活动。
自从我的ContentPage.Content
被设置为ScrollView
,我尝试过设置ScrollOrientation
to 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 的 Intellisense 版本和Xamarin 表单 API 文档查看我可用的不同布局,其中没有一个Orientation
财产。
我担心做到这一点的唯一方法是创建第二个特定平台Activity
就为了这个ContentPage
并将方向设置为横向。
虽然这种方法可行,但它使屏幕之间的导航变得更加复杂。
目前该功能正在 Android 中进行测试。
不想这么说,但这只能使用自定义渲染器或特定于平台的代码
在android中,您可以设置要求的方向MainActivity 的属性ScreenOrientation.Landscape
.
在 iOS 中,您可以覆盖GetSupportedInterfaceOrientations
in the AppDelegate
类返回其中之一UIInterfaceOrientationMask
值时Xamarin.Forms.Application.Current.MainPage
is the 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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)