- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 Xamarin.Forms 创建一个跨平台应用程序,我所有的 ContentPages
都位于 PCL
中。
我正在寻找一种方法来将单个 ContentPage
的 orientation
设置并锁定为 Landscape
,最好无需创建另一个每个平台特定项目中的 Activity 。
由于我的 ContentPage.Content
设置为 ScrollView
,我尝试将 ScrollOrientation
设置为 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 Forms API Doc's浏览可供我使用的不同布局,其中没有一个具有 Orientation
属性。
我担心这样做的唯一方法是为这个 ContentPage
创建第二个特定于平台的 Activity
并将方向设置为横向。
虽然这种方法可行,但它使屏幕之间的导航变得更加复杂。
目前正在 Android 中进行测试。
最佳答案
不想这么说,但这只能使用 custom renderers 来完成或特定于平台的代码
在android中,你可以设置RequestedOrientation将 MainActivity 的属性设置为 ScreenOrientation.Landscape
。
在 iOS 中,当 Xamarin.Forms. Application.Current.MainPage
是您感兴趣的 ContentPage
。
安卓
[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;
}
}
关于android - 如何在 Xamarin.Forms 中设置 ContentPage 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157585/
我是一名优秀的程序员,十分优秀!