Xamarin iOS ObserveOrientationDidChange Landscape 或 Portrait [英] Xamarin iOS ObserveOrientationDidChange Landscape or Portrait

查看:12
本文介绍了Xamarin iOS ObserveOrientationDidChange Landscape 或 Portrait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测用户何时将设备旋转到横向.我可以使用以下代码检测方向何时发生变化:

I need to detect when the user rotate the device to Landscape orientation. I can detect when the orientation changed with the code below:

UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
this.orientationObserver = UIDevice.Notifications.ObserveOrientationDidChange(MyRotationCallback);

private void MyRotationCallback(object sender, NSNotificationEventArgs e)
{
    // Landscape or Portrait ?
}

它可以工作,但我不知道它是改为横向还是纵向.我如何才能检测到这一点?

It works, but I can't tell if it was changed to landscape or portrait. How I can I detect this?

推荐答案

检查属性:

UIDevice.CurrentDevice.Orientation

值:

LandscapeLeft
PortraitUpsideDown
LandscapeRight
Portrait

示例设置:

在您的 ViewDidLoad(或类似的)中:

Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), DeviceRotated);

委托/行动:

void DeviceRotated(NSNotification notification)
{
    System.Diagnostics.Debug.WriteLine(UIDevice.CurrentDevice.Orientation);
}

输出:

LandscapeLeft
PortraitUpsideDown
LandscapeRight
Portrait

这篇关于Xamarin iOS ObserveOrientationDidChange Landscape 或 Portrait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆