How to set ContentPage orientation or screen orien

2019-07-14 06:11发布

问题:

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

I have set ScreenOrientation = ScreenOrientation.

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.

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

回答1:

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

Try doing this:

Interface

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

For 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;
        }
    }
}

For 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();

Hope it helps!



回答2:

So this is how I do it in Android. You have to add the code to MainActivity.cs

[Activity(...., ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

or your app will throw an exception if the user turns his device.

Then add this:

//To check if device is allowed to rotate
private bool _allowLandscape;

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    private bool _allowLandscape;

    protected override void OnCreate(Bundle bundle)
    {
        switch (Device.Idiom)
        {
            case TargetIdiom.Phone:
                RequestedOrientation = ScreenOrientation.Portrait;
                break;
            case TargetIdiom.Tablet:
                RequestedOrientation = ScreenOrientation.Landscape;
                break;
        }

        base.OnCreate(bundle);

        //Setup additional stuff that you need

        //Calls from the view that should rotate
        MessagingCenter.Subscribe<GraphicsView>(this, "graphic", sender =>
        {
            _allowLandscape = true;
            OnConfigurationChanged(new Configuration());
        });

        //When the page is closed this is called
        MessagingCenter.Subscribe<GraphicsView>(this, "return", sender =>
        {
            _allowLandscape = false;
            OnConfigurationChanged(new Configuration());
        });

        LoadApplication(new App());
    }

    public override void OnConfigurationChanged(Configuration newConfig)
    {
        base.OnConfigurationChanged(newConfig);

        switch (newConfig.Orientation)
        {
            case Orientation.Landscape:
                switch (Device.Idiom)
                {
                    case TargetIdiom.Phone:
                        if (!_allowLandscape)
                        {
                            LockRotation(Orientation.Portrait);
                        }
                        break;
                    case TargetIdiom.Tablet:
                        LockRotation(Orientation.Landscape);
                        break;
                }
                break;
            case Orientation.Portrait:
                switch (Device.Idiom)
                {
                    case TargetIdiom.Phone:
                        if (!_allowLandscape)
                        {
                            LockRotation(Orientation.Portrait);
                        }
                        break;
                    case TargetIdiom.Tablet:
                        LockRotation(Orientation.Landscape);
                        break;
                }
                break;
            case Orientation.Undefined:
                if (Device.Idiom == TargetIdiom.Phone && _allowLandscape)
                {
                    LockRotation(Orientation.Landscape);
                }
                else if (Device.Idiom == TargetIdiom.Phone && !_allowLandscape)
                {
                    LockRotation(Orientation.Portrait);
                }
                break;
        }
    }

    private void LockRotation(Orientation orientation)
    {
        switch (orientation)
        {
            case Orientation.Portrait:
                RequestedOrientation = ScreenOrientation.Portrait;
                break;
            case Orientation.Landscape:
                RequestedOrientation = ScreenOrientation.Landscape;
                break;
        }
    }
}

Hope this will work for you.