Xamarin iOS ObserveOrientationDidChange Landscape或Portrait

Dar*_*ius 4 c# xamarin.ios ios xamarin

我需要检测用户何时将设备旋转到横向方向.我可以通过以下代码检测方向何时改变:

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

private void MyRotationCallback(object sender, NSNotificationEventArgs e)
{
    // Landscape or Portrait ?
}
Run Code Online (Sandbox Code Playgroud)

它有效,但我不知道它是否改为横向或纵向.我怎么能发现这个?

Sus*_*ver 10

检查财产:

UIDevice.CurrentDevice.Orientation
Run Code Online (Sandbox Code Playgroud)

价值观:

LandscapeLeft
PortraitUpsideDown
LandscapeRight
Portrait
Run Code Online (Sandbox Code Playgroud)

示例设置:

在你ViewDidLoad(或类似):

Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), DeviceRotated);
Run Code Online (Sandbox Code Playgroud)

代表/行动:

void DeviceRotated(NSNotification notification)
{
    System.Diagnostics.Debug.WriteLine(UIDevice.CurrentDevice.Orientation);
}
Run Code Online (Sandbox Code Playgroud)

输出:

LandscapeLeft
PortraitUpsideDown
LandscapeRight
Portrait
Run Code Online (Sandbox Code Playgroud)