android - 让 xamarin 表单中的一个共享页面保持横向

标签 android ios xamarin

我必须让 xamarin 表单应用程序中的页面进入横向模式并保持横向模式。它是共享项目,在 android 和 iOS 中使用。有人知道实现这一目标的方法吗?

最佳答案

Supun's answer是在正确的轨道上,但它强制纵向模式。如果你想强制横向,这是你要走的路:

ThirdPage.xaml.cs:

public new void OnAppearing()
{
    base.OnAppearing();
    MessagingCenter.Send(this, "PreventPortrait");
}

public new void OnDisappearing()
{
    base.OnDisappearing();
    MessagingCenter.Send(this, "AllowPortrait");
}

安卓:

在 MainActivity 的 OnCreate() 中:

MessagingCenter.Subscribe<ThirdPage>(this, "PreventPortrait", sender =>
{
    RequestedOrientation = ScreenOrientation.Landscape;
});

MessagingCenter.Subscribe<ThirdPage>(this, "AllowPortrait", sender =>
{
    RequestedOrientation = ScreenOrientation.Unspecified;
});

iOS:

AppDelegate.cs:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application,UIWindow forWindow)
{ 
   var mainPage = Xamarin.Forms.Application.Current.MainPage;
   if (mainPage.Navigation.NavigationStack.Last() is ThirdPage)
    {
    return UIInterfaceOrientationMask.AllButUpsideDown;
    }
  return UIInterfaceOrientationMask.Landscape;
}

ThirdPageRenderer.cs:在页面消失时将配置设置回纵向:

[assembly: ExportRenderer(typeof(ThirdPage), typeof(ThirdPageRenderer))]
namespace MyForm.iOS
{
  public class ThirdPageRenderer : PageRenderer
  { 
    public override void ViewWillDisappear(bool animated)
     {
     base.ViewWillDisappear(animated);
     UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.Landscape)), new NSString("orientation")); 
     }
   }
}

关于android - 让 xamarin 表单中的一个共享页面保持横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48817758/

相关文章:

ios - 红框移动到核心动画中 UIImage View 的位置

ios - 将 UICollectionViewCell 滚动到背景而不是垂直

ios - 统计某个对象在 JSON 查询中出现的次数

android - Adreno 220 GLSL 漏洞

java - 适用于不同屏幕尺寸的 Android 处理程序

MvvmCross:应用程序范围的 View 模型?

android - MediaPlayer同时播放多首歌曲 : only one is really playing

c# - 您将如何在 Xamarin C# for iOS 中创建可堆叠的矩形?

android - ExpandableListView 和 NavigationDrawer

javascript - Titanium:使用 Titanium UI 设置文本字段的 ID