Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation

Cross-platform plugin to work with screen orientation of mobile device.
https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation

android csharp-library device-orientation hacktoberfest ios netstandard nuget orientation-lock-plugin screen-orientation uwp xamarin xamarin-android xamarin-forms xamarin-ios xamarin-plugin

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

        

## Device Orientation Plugin for Xamarin and Windows

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

[iOS demo](https://youtu.be/3eQDrHMPmE4)

[Xamarin.Forms sample](https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation/tree/master/samples/Xamarin.Forms-sample)

#### Setup

* Available on NuGet: [![NuGet Badge](https://buildstats.info/nuget/Plugin.DeviceOrientation)](https://www.nuget.org/packages/Plugin.DeviceOrientation/)
* Install into your PCL/NetStandard project and Platform Specific projects

#### Platform Support

|Platform|Version|
| ------------------- | :------------------: |
|Xamarin.iOS|iOS 10+|
|Xamarin.Android|API 19+|
|Windows 10 UWP|10.0.16299+|

v2.0 updated to NetStandard 2.0

#### Deprecated platforms

* Windows Phone Silverlight
* Windows Phone RT
* Windows Store RT

Implementations for unsupported platforms contains [here](https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation/tree/deprecated/src/DeviceOrientation/).

## API Usage

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

```csharp
///
/// Gets current device orientation
///
DeviceOrientations CurrentOrientation { get; }
```

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

```csharp
///
/// Event handler when orientation changes
///
event OrientationChangedEventHandler OrientationChanged;
```

You will get a `OrientationChangedEventArgs` with the orientation type:

```csharp
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:
```csharp
///
/// Lock orientation in the specified position
///
/// Position for lock.
void LockOrientation(DeviceOrientations orientation);
```
For disable the lock, call `UnlockOrientation` method:
```csharp
///
/// Unlock orientation
///
void UnlockOrientation();
```

### iOS Specific Support

Add this code for your ViewController where you want locking orientation:
```csharp
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`:
```csharp
[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:

```csharp
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
* [Android - Handling Rotation](https://developer.xamarin.com/guides/android/application_fundamentals/handling_rotation/)
* [Xamarin.Forms - Device Orientation](https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/device-orientation/)

## Troubleshooting
* [See in the issues](https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation/issues?q=label%3Afaq)

## Contributors
* [Yauheni Pakala](https://github.com/wcoder)

---
© 2016-2019 MIT License