XamarinAPI33Bundle.GetParcelable替代方法
时间:2025-07-28 13:03:31 384浏览 收藏
本文针对 Xamarin.Android 开发者,详细阐述了 Android API 33 中 `Bundle.GetParcelable(string)` 方法被废弃,并推荐使用类型安全的 `GetParcelable(string, Class
理解 Bundle.GetParcelable 的废弃与类型安全
在 Android 开发中,Bundle 是一个常用的数据容器,用于在组件之间(如 Activity 之间)传递数据。传统上,我们使用 Bundle.GetParcelable(string key) 方法来检索存储的 Parcelable 对象。然而,从 Android API 33 (Tiramisu) 开始,此方法已被标记为废弃(@Deprecated),并建议使用新的、类型更安全的 GetParcelable(String key, Class
原有的 GetParcelable(string key) 方法签名如下(摘自 Android 源码):
/* @deprecated Use the type-safer {@link #getParcelable(String, Class)} starting from Android * {@link Build.VERSION_CODES#TIRAMISU}. */ @Deprecated @Nullable publicT getParcelable(@Nullable String key) { // Implementation here }
其主要问题在于它返回一个泛型的 T 类型,但没有在方法签名中强制指定 T 的具体类型,从而可能导致运行时类型转换错误。为了提高类型安全性并减少潜在的运行时异常,新的 API 引入了 Class
新的 GetParcelable(String key, Class
/** * Returns the value associated with the given key or {@code null} if: *
-
*
- No mapping of the desired type exists for the given key. *
- A {@code null} value is explicitly associated with the key. *
- The object is not of type {@code clazz}. *
这个新方法要求调用者显式提供预期的类型信息,从而在编译时就能进行更严格的类型检查,避免了不必要的运行时错误。
Xamarin.Android 中 Parcelable 对象的传递与接收
在 Xamarin.Android 项目中,我们通常会定义一个 C# 类并使其实现 IParcelable 接口(或通过 [Parcelable] 属性自动生成相关代码),以便在 Bundle 中传递。
原始(已废弃)的代码示例:
假设我们有一个名为 User 的 C# 类,它实现了 IParcelable 接口,并且我们通过 Intent 和 Bundle 在活动之间传递 User 对象。
发送 User 对象:
// 假设 MyUser 是一个已填充数据的 User 实例 User MyUser = new User("...", "...", /* ...其他属性... */); Intent intent = new Intent(this, typeof(Menu)); Bundle bundlee = new Bundle(); bundlee.PutParcelable("MyUser", MyUser); // 将 User 实例存入 Bundle intent.PutExtra("TheBundle", bundlee); StartActivity(intent);
请注意,PutParcelable 方法并未废弃,其使用方式保持不变。
接收 User 对象(旧的、已废弃的方式):
// 在目标 Activity 中接收 Bundle Bundle bundlee = Intent.GetBundleExtra("TheBundle"); User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""); // 初始化,但这行在实际使用中通常不是必需的,可以直接赋值 MyUser = bundlee.GetParcelable("MyUser") as User; // 废弃警告出现在这里
上述代码中的 bundlee.GetParcelable("MyUser") 会触发废弃警告,因为它使用的是不带 Class 参数的旧方法。
迁移至类型安全的 GetParcelable 方法
要解决 Bundle.GetParcelable 的废弃问题,我们需要使用新的 GetParcelable(String key, Class
Java.Lang.Class.FromType() 方法能够将一个 .NET System.Type 对象转换为对应的 Java java.lang.Class 对象,这正是 GetParcelable 新方法所需要的。
接收 User 对象(新的、推荐的方式):
// 在目标 Activity 中接收 Bundle Bundle bundlee = Intent.GetBundleExtra("TheBundle"); if (bundlee != null) { // 使用新的 GetParcelable 方法,通过 Java.Lang.Class.FromType 提供类型信息 // MyUser = bundlee.GetParcelable("MyUser", Java.Lang.Class.FromType(typeof(User))) as User; // 或者,如果 MyUser 实例已经存在(如通过默认构造函数创建),也可以使用其类型 // User MyUser = new User(); // 假设 User 有一个无参构造函数 // MyUser = bundlee.GetParcelable("MyUser", Java.Lang.Class.FromType(MyUser.GetType())) as User; // 更简洁和推荐的做法是直接获取并赋值 User MyUser = bundlee.GetParcelable("MyUser", Java.Lang.Class.FromType(typeof(User))) as User; if (MyUser != null) { // 成功获取到 MyUser 对象,进行后续操作 Console.WriteLine($"User Name: {MyUser.Name}"); // 假设 User 类有 Name 属性 } else { Console.WriteLine("Failed to retrieve MyUser object or it was null."); } } else { Console.WriteLine("Bundle is null."); }
在上述代码中,Java.Lang.Class.FromType(typeof(User)) 将 C# 的 User 类型转换为 Java 运行时所需的 Class 对象。这样,GetParcelable 方法就能确保返回的对象是 User 类型或其子类型,从而满足类型安全的要求。
注意事项与总结
- 类型匹配: 确保 Java.Lang.Class.FromType() 中传入的类型与实际存储在 Bundle 中的 Parcelable 对象类型一致。
- 空值检查: 无论使用新旧方法,从 Bundle 中取出的对象都可能为 null(例如,如果键不存在或存储的值就是 null),因此进行空值检查是良好的编程习惯。
- as 运算符: 即使使用了类型安全的 GetParcelable 方法,返回的仍然是泛型 T,在 C# 中为了将其赋值给具体的 User 类型变量,使用 as User 进行类型转换依然是必要的,这有助于在转换失败时返回 null 而不是抛出异常。
- ClassLoader: Android 官方文档提到,如果期望的值不是 Android 平台提供的类,可能需要先调用 Bundle.SetClassLoader(ClassLoader)。但在 Xamarin.Android 中,对于自定义的 Parcelable 类,Java.Lang.Class.FromType 通常会正确处理类加载器的问题,因此在大多数情况下无需手动设置。但如果遇到类找不到的异常,可以考虑检查或设置 ClassLoader。
- 前瞻性: 尽早采纳新的 API 标准,可以避免未来 Android 版本更新可能带来的兼容性问题,并使代码更加健壮和易于维护。
通过上述迁移步骤,您的 Xamarin.Android 应用将能够兼容 Android API 33 及更高版本,并消除 Bundle.GetParcelable 相关的废弃警告,提升代码的质量和前瞻性。
好了,本文到此结束,带大家了解了《XamarinAPI33Bundle.GetParcelable替代方法》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
484 收藏
-
137 收藏
-
394 收藏
-
205 收藏
-
191 收藏
-
277 收藏
-
395 收藏
-
339 收藏
-
351 收藏
-
165 收藏
-
392 收藏
-
410 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习