Skip to content

Instantly share code, notes, and snippets.

@aliozgur
Last active April 4, 2018 07:12
Show Gist options
  • Save aliozgur/8b0d0e4a8dd38b2b17ad to your computer and use it in GitHub Desktop.
Save aliozgur/8b0d0e4a8dd38b2b17ad to your computer and use it in GitHub Desktop.
Xamarin.Forms : Device Orientation Service
using System;
namespace MyNamespace
{
public enum DeviceOrientation
{
Landscape,
Portrait
}
}
using System;
using Xamarin.Forms;
using MyNamespace.Android;
using Android.OS;
using Android.Views;
using Android.Content;
using Android.Runtime;
using Android.App;
[assembly: Dependency(typeof (DeviceOrientationService))]
namespace MyNamespace.Android
{
public class DeviceOrientationService:IDeviceOrientationService
{
#region IDeviceOrientationService implementation
public DeviceOrientation GetOrientation()
{
IWindowManager windowManager = Application.Context .GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
var rotation = windowManager.DefaultDisplay.Rotation;
bool isLandscape = rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270;
return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
}
#endregion
}
}
using System;
using MonoTouch.UIKit;
using Xamarin.Forms;
using MyNamespace.iOS;
[assembly: Dependency(typeof (DeviceOrientationService))]
namespace MyNamespace.iOS
{
public class DeviceOrientationService:IDeviceOrientationService
{
#region IDeviceOrientationService implementation
public DeviceOrientation GetOrientation()
{
var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;
bool isPortrait = currentOrientation == UIInterfaceOrientation.Portrait || currentOrientation == UIInterfaceOrientation.PortraitUpsideDown;
return !isPortrait ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
}
#endregion
}
}
using System;
namespace MyNamespace
{
public interface IDeviceOrientationService
{
DeviceOrientation GetOrientation();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Threading;
namespace MyNamespace
{
public class EnergyChartPage : ContentPage
{
private ChartParamsViewModel _paramsModel;
private EnergyProductionData _model;
private ChartServices _chartSvc;
private IDeviceOrientationService _deviceOrientationSvc;
public EnergyChartPage(ChartParamsViewModel parmsModel)
{
_deviceOrientationSvc = DependencyService.Get<IDeviceOrientationService>();
_chartSvc = new ChartServices();
_paramsModel = parmsModel;
var root = new StackLayout(){ Orientation = StackOrientation.Horizontal };
Content = root;
}
protected override void OnAppearing()
{
base.OnAppearing();
var initialOrientation = _deviceOrientationSvc.GetOrientation();
ApplyOrientation(initialOrientation);
}
private void ApplyOrientation(DeviceOrientation orientation)
{
switch (orientation)
{
case DeviceOrientation.Portrait:
//TODO
break;
case DeviceOrientation.Landscape:
//TODO
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment