如何在Xamarin.Forms中设置ContentPage方向 [英] How to set ContentPage orientation in Xamarin.Forms
问题描述
我正在使用Xamarin.Forms创建跨平台应用程序,我的所有ContentPages
都位于PCL
中.
I'm using Xamarin.Forms to create a cross platform application, all of my ContentPages
are situated within the PCL
.
我正在寻找一种将单个ContentPage
的orientation
设置并锁定为Landscape
的方法,最好不必在每个平台特定项目中创建另一个活动.
I'm looking for a way to set and lock the orientation
of a single ContentPage
to Landscape
, preferably without having to create another activity in each of the platform specific projects.
由于我的ContentPage.Content
设置为ScrollView
,因此我尝试将ScrollOrientation
设置为Horizontal
,但是这不起作用.
Since my ContentPage.Content
is set to a ScrollView
, I've tried setting the ScrollOrientation
to Horizontal
, however this did not work.
我也尝试过使用RelativeLayout
,但在此上看不到Orientation
属性.
I've also tried using a RelativeLayout
, but I can't see an Orientation
property on this.
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 Forms API Doc 查看可供我使用的不同布局,而这些布局都没有Orientation
属性.
The last thing I tried was using Xamarin Studio's version of Intellisense and the Xamarin Forms API Doc's to look through the different Layouts available to me, none of which had a Orientation
property.
我担心这样做的唯一方法是为该ContentPage
创建第二个特定于平台的Activity
并将方向设置为横向.
I fear the only way to do this is by creating a second platform specific Activity
just for this one ContentPage
and setting the orientation to landscape.
尽管此方法可行,但它使屏幕之间的导航更加复杂.
Although this method would work, it makes the Navigation between screens a lot more complex.
目前正在Android上对其进行测试.
This is currently being tested in Android.
推荐答案
Hate to say this but this can only be done using custom renderers or a platform-specific code
在android中,您可以设置 RequestedOrientation 将MainActivity的>属性设置为ScreenOrientation.Landscape
.
In android, you can set the RequestedOrientation property of the MainActivity to ScreenOrientation.Landscape
.
在iOS中,如果Xamarin.Forms.Application.Current.MainPage
是要插入的ContentPage
,则可以覆盖AppDelegate
类中的GetSupportedInterfaceOrientations
以返回UIInterfaceOrientationMask
值之一.
In iOS, you can override GetSupportedInterfaceOrientations
in the AppDelegate
class to return one of the UIInterfaceOrientationMask
values when Xamarin.Forms.Application.Current.MainPage
is the ContentPage
that you are intereted in.
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;
}
}
这篇关于如何在Xamarin.Forms中设置ContentPage方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!