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

Commit 4940927

Browse files
authored
Merge pull request #11 from wcoder/bufixes/forms-android-issue-10
Fixed for Xamarin.Forms Android issue #10
2 parents 44148ef + 4efc181 commit 4940927

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

samples/DO.Samples.Forms/DO.Samples.Forms/DO.Samples.Forms.Android/MainActivity.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,18 @@ protected override void OnCreate(Bundle bundle)
2020
// Step 1:
2121
CrossCurrentActivity.Current.Activity = this;
2222

23-
// Step 2:
24-
DeviceOrientationImplementation.Init();
25-
2623
// ...
2724

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

32-
// Step 3:
29+
// Step 2:
3330
public override void OnConfigurationChanged(Configuration newConfig)
3431
{
3532
base.OnConfigurationChanged(newConfig);
3633

37-
DeviceOrientationImplementation.NotifyOrientationChange(newConfig);
34+
DeviceOrientationImplementation.NotifyOrientationChange(newConfig.Orientation);
3835
}
3936
}
4037
}

samples/DO.Samples.Forms/DO.Samples.Forms/DO.Samples.Forms/MainPage.xaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
xmlns:local="clr-namespace:DO.Samples.Forms"
55
x:Class="DO.Samples.Forms.MainPage">
66

7-
<Label Text="Welcome to Xamarin.Forms!"
8-
VerticalOptions="Center"
9-
HorizontalOptions="Center" />
7+
<Button x:Name="btn"
8+
Text="---------"
9+
VerticalOptions="Center"
10+
HorizontalOptions="Center"
11+
Clicked="Button_Click" />
1012

1113
</ContentPage>

samples/DO.Samples.Forms/DO.Samples.Forms/DO.Samples.Forms/MainPage.xaml.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,38 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Plugin.DeviceOrientation;
7+
using Plugin.DeviceOrientation.Abstractions;
68
using Xamarin.Forms;
79

810
namespace DO.Samples.Forms
911
{
10-
public partial class MainPage : ContentPage
11-
{
12-
public MainPage()
13-
{
14-
InitializeComponent();
12+
public partial class MainPage : ContentPage
13+
{
14+
private bool _isLocked = false;
15+
16+
public MainPage()
17+
{
18+
InitializeComponent();
1519

1620
var a = new DSImage(1, 2, 3, 4);
17-
}
18-
}
21+
22+
CrossDeviceOrientation.Current.OrientationChanged += CurrentOnOrientationChanged;
23+
}
24+
25+
private void CurrentOnOrientationChanged(object sender, OrientationChangedEventArgs orientationChangedEventArgs)
26+
{
27+
btn.Text = orientationChangedEventArgs.Orientation.ToString();
28+
}
29+
30+
public void Button_Click(object sender, EventArgs args)
31+
{
32+
if (_isLocked)
33+
CrossDeviceOrientation.Current.LockOrientation(CrossDeviceOrientation.Current.CurrentOrientation);
34+
else
35+
CrossDeviceOrientation.Current.UnlockOrientation();
36+
37+
_isLocked = !_isLocked;
38+
}
39+
}
1940
}

src/DeviceOrientation/Plugin.DeviceOrientation.Android/DeviceOrientationImplementation.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Android.App;
33
using Android.Content;
44
using Android.Content.PM;
5+
using Android.Content.Res;
56
using Android.Hardware;
67
using Android.Runtime;
78
using Android.Views;
@@ -14,12 +15,37 @@ public class DeviceOrientationImplementation : BaseDeviceOrientationImplementati
1415
{
1516
private readonly OrientationListener _listener;
1617
private bool _disposed;
18+
private bool _isListenerEnabled = true;
19+
20+
protected bool IsListenerEnabled
21+
{
22+
set
23+
{
24+
if (_listener == null) return;
25+
26+
if (value == _isListenerEnabled) return;
27+
28+
if (value)
29+
{
30+
_listener.Enable();
31+
}
32+
else
33+
{
34+
_listener.Disable();
35+
}
36+
37+
_isListenerEnabled = value;
38+
}
39+
}
1740

1841
public DeviceOrientationImplementation()
1942
{
2043
_listener = new OrientationListener(OnOrientationChanged);
44+
2145
if (_listener.CanDetectOrientation())
46+
{
2247
_listener.Enable();
48+
}
2349
}
2450

2551
public override DeviceOrientations CurrentOrientation
@@ -63,6 +89,21 @@ public override void Dispose(bool disposing)
6389
base.Dispose(disposing);
6490
}
6591

92+
public static void NotifyOrientationChange(Orientation newOrientation, bool isForms = true)
93+
{
94+
var instance = (DeviceOrientationImplementation)CrossDeviceOrientation.Current;
95+
96+
if (instance == null)
97+
throw new InvalidCastException("Cast from IDeviceOrientation to Android.DeviceOrientationImplementation");
98+
99+
instance.IsListenerEnabled = !isForms;
100+
101+
instance.OnOrientationChanged(new OrientationChangedEventArgs
102+
{
103+
Orientation = CrossDeviceOrientation.Current.CurrentOrientation
104+
});
105+
}
106+
66107
private ScreenOrientation Convert(DeviceOrientations orientation)
67108
{
68109
switch (orientation)

0 commit comments

Comments
 (0)