c# - 错误 CS1540/CS0122 : Getting keyboard size doesn't work after switching to Unified API

标签 c# ios xamarin.ios xamarin uikeyboard

今天我更新到 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/

相关文章:

iphone - UIWebView 在处理 ajax 投票网站时返回空白

iphone - MonoTouch 的条形扫描仪?

xamarin - Xamarin 会为其所有应用程序携带其运行时吗?

c# - 如何使用 azure 存储表实现每秒 10 次以上的插入

c# - 如何在 F# Giraffe Web API 中检索 url 编码形式?

C# 未从 HttpWebResponse 获得正确的响应。编码?

ios - MBProgressHUD 有时不会隐藏 - Swift

ios - CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用

xamarin.ios - MonoDevelop Intellisense 不显示 PCL 中的所有项目

c# - 使用主线程而不阻塞它的异步方法示例.c#