Giter VIP home page Giter VIP logo

xamarin.plugin.deviceorientation's Introduction

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

  • Available on NuGet: NuGet Badge
  • 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.

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.

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-2019 MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.