Plugin.Maui.DeviceOrientation 1.0.0

dotnet add package Plugin.Maui.DeviceOrientation --version 1.0.0
NuGet\Install-Package Plugin.Maui.DeviceOrientation -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.Maui.DeviceOrientation" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Plugin.Maui.DeviceOrientation --version 1.0.0
#r "nuget: Plugin.Maui.DeviceOrientation, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Plugin.Maui.DeviceOrientation as a Cake Addin
#addin nuget:?package=Plugin.Maui.DeviceOrientation&version=1.0.0

// Install Plugin.Maui.DeviceOrientation as a Cake Tool
#tool nuget:?package=Plugin.Maui.DeviceOrientation&version=1.0.0

Device Orientation Plugin for .Net Maui and Windows

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

Based on Plugin.DeviceOrientation by Yauheni Pakala

iOS demo

Maui sample

Setup
  • Available on NuGet: NuGet Badge
  • Install into your PCL/NetStandard project and Platform Specific projects
Platform Support
Platform Version
iOS iOS 14.2+
Android API 21+
Windows 10 UWP 10.0.16299+

API Usage

Call DeviceOrientation.Default from any project 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.

Member Value Description
Undefined 0 No display orientation is specified.
Landscape 1 Specifies that the monitor is oriented in landscape mode where the width of the display viewing area is greater than the height.
Portrait 2 Specifies 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.
LandscapeFlipped 4 Specifies 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.
PortraitFlipped 8 Specifies 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-2024 MIT License

Product Compatible and additional computed target framework versions.
.NET net8.0-android34.0 is compatible.  net8.0-ios17.2 is compatible.  net8.0-windows10.0.19041 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 128 5/4/2024