7

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
sonu
  • 151
  • 3
  • 7
  • 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 – sonu Feb 17 '17 at 06:19

2 Answers2

14

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!

mindOfAi
  • 4,412
  • 2
  • 16
  • 27
  • where do I put this code ? I am complete new to xamarin – GuidoG Feb 15 '22 at 12:46
  • In case someone is having the "Forms.Context is obsolete" warning in android, the Solution two in here (https://stackoverflow.com/a/59284116/11417475) worked like a charm for me. Just replace ```((Activity)Forms.Context)``` from this answer by the ```MainActivity.Instance``` from the link I mentioned – Gabic May 13 '22 at 14:01
0

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.

testing
  • 19,681
  • 50
  • 236
  • 417
Chandresh Khambhayata
  • 1,748
  • 2
  • 31
  • 60