错误CS1540/CS0122:切换到Unified API后,获取键盘大小不起作用

tes*_*ing 4 c# xamarin.ios uikeyboard ios xamarin

今天我更新到Xamarin.iOS 8.6.0.51并切换到新的Unified API.

现在我想获得键盘大小(此代码之前有效):

var val = new NSValue (notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey).Handle);
RectangleF keyboardSize = val.RectangleFValue;
Run Code Online (Sandbox Code Playgroud)

在迁移工具的帮助下,RectangleF转换为CGRect,但我在这里得到的错误

错误CS1540:无法访问受保护的成员 Foundation.NSValue.NSValue(System.IntPtr)' via a qualifier of type Foundation.NSValue'.限定符必须是"MyApp.SomeViewController"类型或从中派生(CS1540)

错误CS0122:由于其保护级别(CS0122),"Foundation.NSValue.NSValue(System.IntPtr)"无法访问

我怎么解决这个问题?我可以删除new NSValue(...),但RectangleFValue仍然无法工作,我需要更换/另一种方式.

编辑:

根据jonathanpeppers,我修改了我的代码:

NSValue keyboardFrameBegin = (NSValue)notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey);
CGRect keyboardSize = keyboardFrameBegin.CGRectValue;
Run Code Online (Sandbox Code Playgroud)

这不会再出现错误,但我无法进一步测试,因为我正在迁移到Unified API,但仍有一些错误需要纠正.

jon*_*ers 8

你能用一个演员:

var val = (NSValue)notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey);
RectangleF keyboardSize = val.RectangleFValue;
Run Code Online (Sandbox Code Playgroud)

你很少需要在Xamarin.iOS中弄乱手柄.

要绝对确定要转换的内容,调试并查看底层类型是什么:

NSObject val = notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey);
Console.WriteLine("Type: " + val.GetType());
Run Code Online (Sandbox Code Playgroud)