Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Fix for Xamarin.Forms Android issue #10 #11

Merged
merged 3 commits into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ protected override void OnCreate(Bundle bundle)
// Step 1:
CrossCurrentActivity.Current.Activity = this;

// Step 2:
DeviceOrientationImplementation.Init();

// ...

global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}

// Step 3:
// Step 2:
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);

DeviceOrientationImplementation.NotifyOrientationChange(newConfig);
DeviceOrientationImplementation.NotifyOrientationChange(newConfig.Orientation);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
xmlns:local="clr-namespace:DO.Samples.Forms"
x:Class="DO.Samples.Forms.MainPage">

<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button x:Name="btn"
Text="---------"
VerticalOptions="Center"
HorizontalOptions="Center"
Clicked="Button_Click" />

</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,38 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Plugin.DeviceOrientation;
using Plugin.DeviceOrientation.Abstractions;
using Xamarin.Forms;

namespace DO.Samples.Forms
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
public partial class MainPage : ContentPage
{
private bool _isLocked = false;

public MainPage()
{
InitializeComponent();

var a = new DSImage(1, 2, 3, 4);
}
}

CrossDeviceOrientation.Current.OrientationChanged += CurrentOnOrientationChanged;
}

private void CurrentOnOrientationChanged(object sender, OrientationChangedEventArgs orientationChangedEventArgs)
{
btn.Text = orientationChangedEventArgs.Orientation.ToString();
}

public void Button_Click(object sender, EventArgs args)
{
if (_isLocked)
CrossDeviceOrientation.Current.LockOrientation(CrossDeviceOrientation.Current.CurrentOrientation);
else
CrossDeviceOrientation.Current.UnlockOrientation();

_isLocked = !_isLocked;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Content.Res;
using Android.Hardware;
using Android.Runtime;
using Android.Views;
Expand All @@ -14,12 +15,37 @@ public class DeviceOrientationImplementation : BaseDeviceOrientationImplementati
{
private readonly OrientationListener _listener;
private bool _disposed;
private bool _isListenerEnabled = true;

protected bool IsListenerEnabled
{
set
{
if (_listener == null) return;

if (value == _isListenerEnabled) return;

if (value)
{
_listener.Enable();
}
else
{
_listener.Disable();
}

_isListenerEnabled = value;
}
}

public DeviceOrientationImplementation()
{
_listener = new OrientationListener(OnOrientationChanged);

if (_listener.CanDetectOrientation())
{
_listener.Enable();
}
}

public override DeviceOrientations CurrentOrientation
Expand Down Expand Up @@ -63,6 +89,21 @@ public override void Dispose(bool disposing)
base.Dispose(disposing);
}

public static void NotifyOrientationChange(Orientation newOrientation, bool isForms = true)
{
var instance = (DeviceOrientationImplementation)CrossDeviceOrientation.Current;

if (instance == null)
throw new InvalidCastException("Cast from IDeviceOrientation to Android.DeviceOrientationImplementation");

instance.IsListenerEnabled = !isForms;

instance.OnOrientationChanged(new OrientationChangedEventArgs
{
Orientation = CrossDeviceOrientation.Current.CurrentOrientation
});
}

private ScreenOrientation Convert(DeviceOrientations orientation)
{
switch (orientation)
Expand Down