本文介绍了如何在Xamarin.Forms中检测设备的屏幕方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检测Xamarin Forms中的屏幕方向变化?我需要更改方向更改的视图,我应该怎么做才能触发方向更改的更改?提供链接或示例代码将非常有帮助.
How to detect screen orientation change in Xamarin Forms? I need to change the view on orientation change, what should I do to trigger a change on orientation change? Providing a link or sample code would be very helpful.
预先感谢
推荐答案
我尝试了依赖注入,但没有成功.现在,我这样解决了:
I tried it with dependency injection, but no succes yet. Right now, I solved it like this:
- 使用
OnSizeAllocated(double, double)
可以在方向更改时更改屏幕. - 使用Singleton存储当前方向.
- Use
OnSizeAllocated(double, double)
for changing the screen on orientation changes. - Use a Singleton for storing the current orientation.
Xamarin.Forms页面
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (DeviceInfo.IsOrientationPortrait() && width > height || !DeviceInfo.IsOrientationPortrait() && width < height)
{
// Orientation got changed! Do your changes here
}
}
单人课程
public class DeviceInfo
{
protected static DeviceInfo _instance;
double width;
double height;
static DeviceInfo()
{
_instance = new DeviceInfo();
}
protected DeviceInfo()
{
}
public static bool IsOrientationPortrait()
{
return _instance.height > _instance.width;
}
public static void SetSize(double width, double height)
{
_instance.width = width;
_instance.height = height;
}
}
这篇关于如何在Xamarin.Forms中检测设备的屏幕方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!