c# - 如何在 Xamarin.Forms 跨平台的特定页面上设置 ContentPage 方向或屏幕方向

标签 c# xamarin xamarin.forms cross-platform orientation

如何在 Xamarin.Forms 跨平台的特定页面上设置内容页面方向或屏幕方向。

我已经设置了 ScreenOrientation = ScreenOrientation

Portrait at property of all platform 但我想显示一些页面显示在两种变体中意味着纵向和横向,那么如何在 `xamarin.forms 的页面上设置。

在 xamarin forms 跨平台中设置屏幕方向不是设备方向而是页面方向一些页面以横向显示,一些页面以纵向显示

最佳答案

您可以通过为特定平台使用 DependencyService 创建依赖项来实现此目的。

尝试这样做:

界面

public interface IOrientationService
    {
        void Landscape();
        void Portrait();
    }

对于安卓:

[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
    public class OrientationService: IOrientationService
    {
        public void Landscape()
        {
            ((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
        }

        public void portrait()
        {
            ((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
        }
    }
}

对于 iOS:

[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
    public class OrientationService: IOrientationService
    {
        public void Landscape()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
        }

        public void Portrait()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
        }
    }
}

然后就可以这样使用了

DependencyService.Get<IOrientationService>().Landscape();
DependencyService.Get<IOrientationService>().Portrait();

希望对您有所帮助!

关于c# - 如何在 Xamarin.Forms 跨平台的特定页面上设置 ContentPage 方向或屏幕方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290561/

相关文章:

android - Xamarin.Forms 从设备特定代码获取数据返回到表单

c# - 在 C# 中使用 Google REST API 刷新 Google token

ios - 与 native 环境相比,在 Windows 上使用 Visual Studio 进行 iPhone 应用程序开发的优势

c# - 将 16 位亮度值转换为 32 位 RGB

xamarin - System.Net.Http.HttpRequestException 异常将在物理设备上运行 xamarin UI 测试

ios - 当 iOS 应用程序在 Xamarin 中作为后台时,如何通知我蓝牙连接?

c# - 如何在 Xamarin.Forms 中创建多列 ListView?

android - Xamarin Android - 操作栏图标填充

c# - 与 COM 引用(版本 1.0 类型库)和 VS 2010 相关的生成警告

c# - 使用 Url Rewriting Net 删除尾部正斜杠