如何在Xamarin.Forms跨平台的特定页面上设置ContentPage方向或屏幕方向 [英] How to set ContentPage orientation or screen orientation on particular page in Xamarin.Forms cross platform

查看:361
本文介绍了如何在Xamarin.Forms跨平台的特定页面上设置ContentPage方向或屏幕方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to set Content-Page orientation or screen orientation on particular page in Xamarin.Forms cross platform.

我设置了ScreenOrientation = ScreenOrientation.

肖像在所有平台的属性中显示,但我想在某些版本中同时显示一些页面显示,分别表示肖像和风景,因此如何在xamarin.forms中设置页面.

Portrait at property of all platform but I want to show some page show in both variation means Portrait and Landscape, so how to set on page in `xamarin.forms.

不是以设备为单位设置屏幕方向,而是以页面为单位设置. xamarin形式的肖像中某些页面跨平台显示

set screen orientation not device wise but set on page-wise some pages show in Landscape & some page show in Portrait in xamarin forms cross- platform

推荐答案

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

You can do this by creating a dependency using DependencyService for specific platforms.

尝试这样做:

接口

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

对于Android:

[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"));
        }
    }
}

然后您可以像这样使用它

Then you can use it like this

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

希望有帮助!

这篇关于如何在Xamarin.Forms跨平台的特定页面上设置ContentPage方向或屏幕方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆