今天我更新到 Xamarin.iOS 8.6.0.51 并切换到新的 Unified API
。
现在我想获取键盘大小(此代码之前有效):
var val = new NSValue (notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey).Handle);
RectangleF keyboardSize = val.RectangleFValue;
在迁移工具的帮助下,RectangleF
被转换为 CGRect
,但我得到的错误是
Error CS1540: Cannot access protected member
Foundation.NSValue.NSValue(System.IntPtr)' via a qualifier of type
Foundation.NSValue'. The qualifier must be of type `MyApp.SomeViewController' or derived from it (CS1540)
和
Error CS0122: `Foundation.NSValue.NSValue(System.IntPtr)' is inaccessible due to its protection level (CS0122)
我该如何解决这个问题?我可以删除 new NSValue(...)
,但 RectangleFValue
仍然不起作用,我需要替换/其他方式。
编辑:
根据 jonathanpeppers 的说法,我将代码修改为:
NSValue keyboardFrameBegin = (NSValue)notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey);
CGRect keyboardSize = keyboardFrameBegin.CGRectValue;
这不会再引发错误,但我无法进一步测试它,因为我正在迁移到 Unified API,但仍有一些错误需要更正。
最佳答案
你能不能只用一个类型转换:
var val = (NSValue)notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey);
RectangleF keyboardSize = val.RectangleFValue;
在 Xamarin.iOS 中,您很少需要弄乱句柄。
要绝对确定要转换为什么,首先调试并查看底层类型是什么:
NSObject val = notification.UserInfo.ValueForKey (UIKeyboard.FrameBeginUserInfoKey);
Console.WriteLine("Type: " + val.GetType());
关于c# - 错误 CS1540/CS0122 : Getting keyboard size doesn't work after switching to Unified API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27922731/