使用通知,我可以在它发生之前检索方向更改

Using a notification with which I retrieve the orientation change before it occurs

因为 UIDeviceOrientationDidChangeNotification 对我来说太晚了 我想我用 UIApplicationWillChangeStatusBarOrientationNotification.

我是这样订阅的:

observer = NSNotificationCenter.DefaultCenter.AddObserver ((NSString)UIApplication.WillChangeStatusBarOrientationNotification, UpdateIntrinsicConstraints);

现在我想提取方向

public void UpdateIntrinsicConstraints(NSNotification notification){
    // Cannot convert type 'Foundation.NSObject' to 'UIKit.UIInterfaceOrientation'
    switch ((UIInterfaceOrientation)notification.UserInfo.ValueForKey(UIApplication.StatusBarOrientationUserInfoKey))
    {
        case UIInterfaceOrientation.LandscapeLeft:
            // ...
            break;
        default:
            break;
    }
}

,但我无法从 NSObject 转换为 UIInterfaceOrientation

UIApplicationWillChangeStatusBarOrientationNotification 适合我吗?如何提取界面旋转的方向(如 willRotateToInterfaceOrientation)?

实际上你从中得到了什么

notification.UserInfo.ValueForKey(UIApplication.StatusBarOrientationUserInfoKey)

是一个NSNumber。所以你必须得到 NSNumber 的值作为 Int。然后您可以将该 int 转换为 UIInterfaceOrientation。 代码中的相同内容:

short shortValue = ((NSNumber)notification.UserInfo.ValueForKey(UIApplication.StatusBarOrientationUserInfoKey)).Int16Value;
UIInterfaceOrientation orientation = ((UIInterfaceOrientation)shortValue);
switch (orientation)
 {
     case UIInterfaceOrientation.LandscapeLeft:
         // ...
         break;
      default:
         break;
 }

或者如果您喜欢一行中的相同内容:

switch ((UIInterfaceOrientation)((NSNumber)notification.UserInfo.ValueForKey(UIApplication.StatusBarOrientationUserInfoKey)).Int16Value)