Home

Awesome

Device Orientation Plugin for Xamarin and Windows

Simple cross-platform plugin to work with screen orientation of mobile device.

iOS demo

Xamarin.Forms sample

Setup

Platform Support

PlatformVersion
Xamarin.iOSiOS 10+
Xamarin.AndroidAPI 19+
Windows 10 UWP10.0.16299+

v2.0 updated to NetStandard 2.0

Deprecated platforms

Implementations for unsupported platforms contains here.

API Usage

Call CrossDeviceOrientation.Current from any project or PCL to gain access to APIs.

/// <summary>
/// Gets current device orientation
/// </summary>
DeviceOrientations CurrentOrientation { get; }

When device orientation is changed you can register for an event to fire:

/// <summary>
/// Event handler when orientation changes
/// </summary>
event OrientationChangedEventHandler OrientationChanged;

You will get a OrientationChangedEventArgs with the orientation type:

public class OrientationChangedEventArgs : EventArgs
{
	public DeviceOrientations Orientation { get; set; }
}

public delegate void OrientationChangedEventHandler(object sender, OrientationChangedEventArgs e);

The DeviceOrientations enumeration has these members.

MemberValueDescription
Undefined0No display orientation is specified.
Landscape1Specifies that the monitor is oriented in landscape mode where the width of the display viewing area is greater than the height.
Portrait2Specifies that the monitor rotated 90 degrees in the clockwise direction to orient the display in portrait mode where the height of the display viewing area is greater than the width.
LandscapeFlipped4Specifies that the monitor rotated another 90 degrees in the clockwise direction (to equal 180 degrees) to orient the display in landscape mode where the width of the display viewing area is greater than the height. This landscape mode is flipped 180 degrees from the Landscape mode.
PortraitFlipped8Specifies that the monitor rotated another 90 degrees in the clockwise direction (to equal 270 degrees) to orient the display in portrait mode where the height of the display viewing area is greater than the width. This portrait mode is flipped 180 degrees from the Portrait mode.

Call LockOrientation for set orientation and lock with disabling device auto-rotation:

/// <summary>
///     Lock orientation in the specified position
/// </summary>
/// <param name="orientation">Position for lock.</param>
void LockOrientation(DeviceOrientations orientation);

For disable the lock, call UnlockOrientation method:

/// <summary>
///     Unlock orientation
/// </summary>
void UnlockOrientation();

iOS Specific Support

Add this code for your ViewController where you want locking orientation:

public override bool ShouldAutorotate()
{
	// set plugin for handle of this method
	return DeviceOrientationImplementation.ShouldAutorotate;
}

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
	// allow all orientations
	return UIInterfaceOrientationMask.AllButUpsideDown;
}

In your Info.plist need set all device orientations.

Xamarin.Forms iOS

If you want to lock orientation for the entire iOS application.

Add this code in your AppDelegate.cs:

[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
{
    return DeviceOrientationImplementation.SupportedInterfaceOrientations;
}

Note: In this case, to lock/unlock orientation on the one screen, you must use the LockOrientation/UnlockOrientation methods.

Xamarin.Forms Android

In your MainActivity.cs, add overriding for changing orientation as here:

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

    DeviceOrientationImplementation.NotifyOrientationChange(newConfig.Orientation);
}

Note: This solution has only two state Portrait and Landscape.

Additional information

Troubleshooting

Contributors


© 2016-2019 MIT License