问题描述
限时送ChatGPT账号..我正在使用 Xamarin.Forms 构建多页应用程序.基本上,这是一个非旋转设置.
I am building a multi-page application with Xamarin.Forms. Basically, this is a non-rotating setup.
[Activity(ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
但是,我们只想在相机页面上获取设备的旋转状态.所以,在camera页面,我们调用如下方法
However, we want to get the rotation status of the device only on the camera page. So, on the camera page, we call the following method
private int GetDeviceRotation()
{
var activity = (Android.App.Activity)this.owner.Activity;
activity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Sensor;
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var rotation = mainDisplayInfo.Rotation == DisplayRotation.Rotation0 ? 0 :
mainDisplayInfo.Rotation == DisplayRotation.Rotation90 ? 90 :
mainDisplayInfo.Rotation == DisplayRotation.Rotation180 ? 180 : 270;
activity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
return rotation;
}
但是,使用这种方法,在拍照时屏幕会旋转片刻.无需旋转屏幕.
However, with this method, the screen rotates for a moment when the picture is taken. There is no need to rotate the screen.
我只想在不旋转屏幕的情况下获取设备的旋转状态.
I only want to get the rotation status of the device without rotating the screen.
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var rotation = mainDisplayInfo.Rotation;
如果您只使用 mainDisplayInfo 而没有 activity.RequestedOrientation,则旋转始终为 0.请帮我.感谢阅读.
rotation is always 0 if you only use mainDisplayInfo without activity.RequestedOrientation. Please help me. Thanks for reading.
推荐答案
您可以使用 OrientationEventListener
做到这一点.
You could do this with OrientationEventListener
.
自定义一个 MyOrientationEventListener
,它扩展了 OrientationEventListener
并在 onCreate
方法中实例化它.
Custom a MyOrientationEventListener
which extends OrientationEventListener
and instantiate it in onCreate
method.
[Activity(ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
private OrientationEventListener orientationEventListener;
public static int currentOrientation;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
...
orientationEventListener = new MyOrientationEventListener(this);
}
class MyOrientationEventListener : OrientationEventListener
{
public MyOrientationEventListener(Context context):base(context)
{
}
public override void OnOrientationChanged(int orientation)
{
if (orientation >= 330 || orientation < 30)
{
currentOrientation = (int)SurfaceOrientation.Rotation0;
}
else if (orientation >= 60 && orientation < 120)
{
currentOrientation = (int)SurfaceOrientation.Rotation90;
}
else if (orientation >= 150 && orientation < 210)
{
currentOrientation = (int)SurfaceOrientation.Rotation180;
}
else if (orientation >= 240 && orientation < 300)
{
currentOrientation = (int)SurfaceOrientation.Rotation270;
}
}
}
//Enables and disables listening
protected override void OnResume()
{
base.OnResume();
orientationEventListener.Enable();
}
protected override void OnPause()
{
base.OnPause();
orientationEventListener.Disable();
}
}
这篇关于我只想在 Xamarin.Forms 不旋转屏幕的情况下获取设备的旋转状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论