This repository was archived by the owner on Mar 26, 2025. It is now read-only.
File tree 4 files changed +76
-15
lines changed
samples/DO.Samples.Forms/DO.Samples.Forms
src/DeviceOrientation/Plugin.DeviceOrientation.Android
4 files changed +76
-15
lines changed Original file line number Diff line number Diff line change @@ -20,21 +20,18 @@ protected override void OnCreate(Bundle bundle)
20
20
// Step 1:
21
21
CrossCurrentActivity . Current . Activity = this ;
22
22
23
- // Step 2:
24
- DeviceOrientationImplementation . Init ( ) ;
25
-
26
23
// ...
27
24
28
25
global ::Xamarin . Forms . Forms . Init ( this , bundle ) ;
29
26
LoadApplication ( new App ( ) ) ;
30
27
}
31
28
32
- // Step 3 :
29
+ // Step 2 :
33
30
public override void OnConfigurationChanged ( Configuration newConfig )
34
31
{
35
32
base . OnConfigurationChanged ( newConfig ) ;
36
33
37
- DeviceOrientationImplementation . NotifyOrientationChange ( newConfig ) ;
34
+ DeviceOrientationImplementation . NotifyOrientationChange ( newConfig . Orientation ) ;
38
35
}
39
36
}
40
37
}
Original file line number Diff line number Diff line change 4
4
xmlns : local =" clr-namespace:DO.Samples.Forms"
5
5
x : Class =" DO.Samples.Forms.MainPage" >
6
6
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" />
10
12
11
13
</ContentPage >
Original file line number Diff line number Diff line change 3
3
using System . Linq ;
4
4
using System . Text ;
5
5
using System . Threading . Tasks ;
6
+ using Plugin . DeviceOrientation ;
7
+ using Plugin . DeviceOrientation . Abstractions ;
6
8
using Xamarin . Forms ;
7
9
8
10
namespace DO . Samples . Forms
9
11
{
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 ( ) ;
15
19
16
20
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
+ }
19
40
}
Original file line number Diff line number Diff line change 2
2
using Android . App ;
3
3
using Android . Content ;
4
4
using Android . Content . PM ;
5
+ using Android . Content . Res ;
5
6
using Android . Hardware ;
6
7
using Android . Runtime ;
7
8
using Android . Views ;
@@ -14,12 +15,37 @@ public class DeviceOrientationImplementation : BaseDeviceOrientationImplementati
14
15
{
15
16
private readonly OrientationListener _listener ;
16
17
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
+ }
17
40
18
41
public DeviceOrientationImplementation ( )
19
42
{
20
43
_listener = new OrientationListener ( OnOrientationChanged ) ;
44
+
21
45
if ( _listener . CanDetectOrientation ( ) )
46
+ {
22
47
_listener . Enable ( ) ;
48
+ }
23
49
}
24
50
25
51
public override DeviceOrientations CurrentOrientation
@@ -63,6 +89,21 @@ public override void Dispose(bool disposing)
63
89
base . Dispose ( disposing ) ;
64
90
}
65
91
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
+
66
107
private ScreenOrientation Convert ( DeviceOrientations orientation )
67
108
{
68
109
switch ( orientation )
You can’t perform that action at this time.
0 commit comments