带有 Xamarin 的 Android 10Android 10 with Xamarin

如何开始使用 Xamarin 开发适用于 Android 10 的应用。How to get started developing apps for Android 10 using Xamarin.Android.

Android 10 现在可从 Google 获取。Android 10 is now available from Google. 此版本提供了许多新功能和 Api,其中许多新功能是在最新 Android 设备中利用新硬件功能所必需的。A number of new features and APIs are being made available in this release, and many of them are necessary to take advantage of new hardware capabilities in the latest Android devices.

Android 10 徽标

本文旨在帮助你开始开发适用于 Android 10 的 Xamarin Android 应用。This article is structured to help you get started in developing Xamarin.Android apps for Android 10. 它说明了如何安装所需的更新、如何配置 SDK,并准备好用于测试的模拟器或设备。It explains how to install the necessary updates, configure the SDK, and prepare an emulator or device for testing. 它还提供了 Android 10 中新增功能的概述,并提供了演示如何使用某些主要 Android 10 功能的示例源代码。It also provides an outline of the new features in Android 10 and provides example source code that illustrates how to use some of the key Android 10 features.

Xamarin 10.0 提供对 Android 10 的支持。Xamarin.Android 10.0 provides support for Android 10. 有关适用于 Android 10 的 Xamarin Android 支持的详细信息,请参阅 " xamarin 10.0 发行说明"。For more information about Xamarin.Android support for Android 10, see the Xamarin.Android 10.0 release notes.

要求Requirements

以下列表是在基于 Xamarin 的应用中使用 Android 10 功能所必需的:The following list is required to use Android 10 features in Xamarin-based apps:

  • Visual studio -建议使用 visual studio 2019。Visual Studio - Visual Studio 2019 is recommended. 在 Windows update 上,Visual Studio 2019 版本16.3 或更高版本。On Windows update to Visual Studio 2019 version 16.3 or later. 在 macOS 上,更新为适用于 Mac 版本8.3 或更高版本的 Visual Studio 2019。On macOS, update to Visual Studio 2019 for Mac version 8.3 or later.
  • Xamarin -xamarin 10.0 或更高版本必须随 visual studio 一起安装(在 Windows 上,将在移动开发中自动安装 xamarin,并将其作为Visual Studio 的一部分安装对于 Mac 安装程序Xamarin.Android - Xamarin.Android 10.0 or later must be installed with Visual Studio (Xamarin.Android is automatically installed as part of the Mobile Development With .NET workload on Windows and installed as part of the Visual Studio for Mac Installer)
  • Java 开发人员工具包-Xamarin 10.0 开发需要 JDK 8。Java Developer Kit - Xamarin.Android 10.0 development requires JDK 8. Microsoft 的 OpenJDK 分发版将作为 Visual Studio 的一部分自动安装。Microsoft's distribution of the OpenJDK is automatically installed as part of Visual Studio.
  • 必须通过 Android SDK 管理器安装 Android SDK API 29 Android SDKAndroid SDK - Android SDK API 29 must be installed via the Android SDK Manager.

入门Get started

若要开始通过 Xamarin 开发 Android 10 应用,必须先下载并安装最新的工具和 SDK 包,然后才能创建第一个 Android 10 项目:To get started developing Android 10 apps with Xamarin.Android, you must download and install the latest tools and SDK packages before you can create your first Android 10 project:

  1. 建议使用 Visual Studio 2019Visual Studio 2019 is recommended. 更新到 Visual Studio 2019 版本16.3 或更高版本。Update to Visual Studio 2019 version 16.3 or later. 如果使用 Visual Studio for Mac 2019,请更新到 Visual Studio 2019 for Mac 版本8.3 或更高版本。If you are using Visual Studio for Mac 2019, update to Visual Studio 2019 for Mac version 8.3 or later.
  2. 通过 SDK 管理器安装Android 10 (API 29) 包和工具。Install Android 10 (API 29) packages and tools via the SDK Manager.
    • Android 10 (API 29) SDK 平台Android 10 (API 29) SDK Platform
    • Android 10 (API 29)系统映像Android 10 (API 29) System Image
    • Android SDK 版本-工具 29.0.0 +Android SDK Build-Tools 29.0.0+
    • Android SDK 平台-工具 29.0.0 +Android SDK Platform-Tools 29.0.0+
    • Android Emulator 29.0.0 +Android Emulator 29.0.0+
  3. 创建面向 Android 10.0 的新 Xamarin 项目。Create a new Xamarin.Android project that targets Android 10.0.
  4. 配置仿真程序或设备以便测试 Android 10 应用。Configure an emulator or device for testing Android 10 apps.

下面介绍了其中的每个步骤:Each of these steps is explained below:

更新 Visual StudioUpdate Visual Studio

建议使用 Visual Studio 2019 通过 Xamarin 生成 Android 10 应用。Visual Studio 2019 is recommended for building Android 10 apps using Xamarin.

如果你使用的是 Visual Studio 2019,请更新到 Visual Studio 2019 版本16.3 或更高版本(有关说明,请参阅将 Visual studio 2019 更新到最新版本)。If you are using Visual Studio 2019, update to Visual Studio 2019 version 16.3 or later (for instructions, see Update Visual Studio 2019 to the most recent release). 在 macOS 上,更新到 Visual Studio 2019 for Mac 8.3 或更高版本(有关说明,请参阅将 Visual studio 2019 For Mac 更新到最新版本)。On macOS, update to Visual Studio 2019 for Mac 8.3 or later (for instructions, see Update Visual Studio 2019 for Mac to the most recent release).

安装 Android SDKInstall the Android SDK

若要使用 Xamarin 10.0 创建项目,必须首先使用 Android SDK 管理器安装适用于 Android 10 的 SDK 平台 (API 级别29)To create a project with Xamarin.Android 10.0, you must first use the Android SDK Manager to install the SDK platform for Android 10 (API level 29).

  1. 启动 SDK 管理器。Start the SDK Manager. 在 Visual Studio 中,单击 "工具" > Android > Android SDK 管理器 "。In Visual Studio, click Tools > Android > Android SDK Manager. 在 Visual Studio for Mac 中,单击 "工具" > "SDK 管理器"。In Visual Studio for Mac, click Tools > SDK Manager.

  2. 在右下角,单击齿轮图标,并选择 "存储库 > Google (不支持)":In the lower right-hand corner, click the gear icon and select Repository > Google (Unsupported):

    Android SDK 管理器存储库选择

  3. 安装Android 10 SDK 平台包(在 "平台" 选项卡中作为Android SDK 平台 29列出)(有关使用 SDK 管理器的详细信息,请参阅Android SDK 安装):Install the Android 10 SDK Platform packages, which are listed as Android SDK Platform 29 in the Platforms tab (for more information about using the SDK Manager, see Android SDK setup):

    Android SDK Manager 平台选项卡

创建 Xamarin Android 项目Create a Xamarin.Android project

创建新的 Xamarin Android 项目。Create a new Xamarin.Android project. 如果不熟悉通过 Xamarin 进行 Android 开发,请参阅Hello,Android了解如何创建 xamarin Android 项目。If you are new to Android development with Xamarin, see Hello, Android to learn about creating Xamarin.Android projects.

创建 Android 项目时,必须将版本设置配置为面向 Android 10.0 或更高版本。When you create an Android project, you must configure the version settings to target Android 10.0 or later. 例如,若要以适用于 Android 10 的项目为目标,必须将项目的目标 Android API 级别配置为android 10.0 (API 29)For example, to target your project for Android 10, you must configure the target Android API level of your project to Android 10.0 (API 29). 这包括目标 Framework 版本目标 ANDROID SDK 版本到 API 29 或更高版本。This includes both your Target Framework Version and Target Android SDK Version to API 29 or later. 有关配置 Android API 级别的详细信息,请参阅了解 ANDROID Api 级别。For more information about configuring Android API levels, see Understanding Android API Levels.

Xamarin Android 目标框架

配置设备或仿真程序Configure a device or emulator

如果你使用的是物理设备,如像素,则可以通过转到 "System" > 在手机的设置中 System update > Check for update 来下载 Android 10 更新。If you are using a physical device such as a Pixel, you can download the Android 10 update by going to the System > System update > Check for update in your phone's settings. 如果你想要闪现设备,请参阅在设备上闪烁出厂映像OTA 映像的说明。If you'd prefer to flash your device, please see the instructions on flashing a Factory Image or OTA Image to your device.

如果使用的是仿真程序,请为 API 级别29创建虚拟设备,并选择基于 x86 的映像。If you are using an emulator, create a virtual device for API level 29 and select an x86-based image. 有关使用 Android Device Manager 创建和管理虚拟设备的信息,请参阅使用 Android Device Manager 管理虚拟设备。For information about using the Android Device Manager to create and manage virtual devices, see Managing Virtual Devices with the Android Device Manager. 有关使用 Android Emulator 进行测试和调试的信息,请参阅Android Emulator 上的调试。For information about using the Android Emulator for testing and debugging, see Debugging on the Android Emulator.

新增功能New features

Android 10 引入了多种新功能。Android 10 introduces a variety of new features. 其中一些新功能旨在利用最新 Android 设备提供的新硬件功能,而其他功能旨在进一步增强 Android 用户体验:Some of these new features are intended to leverage new hardware capabilities offered by the latest Android devices, while others are designed to further enhance the Android user experience:

利用 Android 10 功能和 Api 增强你的应用Enhance your app with Android 10 features and APIs

接下来,当你准备就绪时,深入了解 Android 10 并了解你可以使用的 新功能和 api 。Next, when you're ready, dive into Android 10 and learn about the new features and APIs that you can use. 下面是一些入门的主要功能。Here are some of the top features to get started with.

对于每个应用,建议采用以下功能:These features are recommend for every app:

  • 深色主题:  确保通过添加 深色主题 或启用 " 强制深色" 来启用系统级深色主题的用户的体验是一致的。Dark Theme: Ensure a consistent experience for users who enable system-wide dark theme by adding a Dark Theme or enabling Force Dark.

深色主题

  • 在应用中支持 gestural 导航  ,方法是:转到边缘,并确保自定义笔势与系统导航手势互补。Support gestural navigation in your app by going edge-to-edge and making sure your custom gestures are complementary to the system navigation gestures.

手势导航

  • 针对 foldables 进行优化: 通过 针对 foldables 进行优化, 提供当今创新设备上无缝的边缘到边缘体验。Optimize for foldables: Deliver seamless, edge-to-edge experiences on today’s innovative devices by optimizing for foldables.

可折叠

如果与应用相关,建议使用以下功能:These features are recommended if relevant for your app:

  • 更多交互式通知:  如果你的通知包括消息,请 在通知中启用建议的答复和操作 以使用户参与,并让他们立即采取措施。More interactive notifications: If your notifications include messages, enable suggested replies and actions in notifications to engage users and let them take action instantly.
  • 更好的生物识别:  如果你使用生物识别身份验证,请转到 BiometricPrompt,这是支持新式设备上指纹身份验证的首选方式。Better biometrics: If you use biometric auth, move to BiometricPrompt, the preferred way to support fingerprint auth on modern devices.
  • 丰富的记录:  支持字幕或游戏录制,请启用 音频播放捕获Enriched recording: To support captioning or gameplay recording, enable audio playback capture. 这是一个很好的方法,可让用户更方便地访问用户并使应用更易于访问。It’s a great way to reach more users and make your app more accessible.
  • 更好的编解码器:  媒体应用程序,请尝试 AV1 用于视频流式处理和 HDR10 + ,实现高动态范围视频。Better codecs: For media apps, try AV1 for video streaming and HDR10+ for high dynamic range video. 对于语音和音乐流,可以使用 Opus编码,对于音乐家,可使用 本机 MIDI API 。For speech and music streaming, you can use Opus encoding, and for musicians, a native MIDI API is available.
  • 更好的网络 api:  如果你的应用程序通过 Wi-fi 管理 IoT 设备,请尝试为配置、下载或打印等功能 新的 网络连接 apiBetter networking APIs: If your app manages IoT devices over Wi-Fi, try the new network connection APIs for functions like configuring, downloading, or printing.

这只是 Android 10 中许多新功能和 Api 的一部分。These are just a few of the many new features and APIs in Android 10. 若要查看所有这些资源,请访问 适用于开发人员的 Android 10 站点To see them all, visit the Android 10 site for developers.

行为变更Behavior changes

如果将 "目标 Android 版本" 设置为 "API 级别 29",即使未实现上述新功能,cann 也会影响应用程序的行为。When the Target Android Version is set to API level 29, there are several platform changes that cann affect your app's behavior even if you are not implementing the new features described above. 以下列表概述了这些更改:The following list is a brief summary of these changes:

总结Summary

本文介绍了 Android 10,并说明了如何安装和配置最新的工具和包,以便安装和配置适用于适用于 android 的 Xamarin Android 开发。This article introduced Android 10 and explained how to install and configure the latest tools and packages for Xamarin.Android development with Android 10. 其中提供了 Android 10 中提供的主要功能的概述。It provided an overview of the key features available in Android 10. 它包含指向 API 文档和 Android 开发人员主题的链接,可帮助你开始创建适用于 Android 10 的应用。It included links to API documentation and Android Developer topics to help you get started in creating apps for Android 10. 它还重点介绍了可能影响现有应用的最重要的 Android 10 行为更改。It also highlighted the most important Android 10 behavior changes that could impact existing apps.