Xamarin.Essentials:手电筒Xamarin.Essentials: Flashlight

Flashlight 类,此类使你能够打开或关闭设备的照相机闪光灯,将其转换为一个手电筒。The Flashlight class has the ability to turn on or off the device's camera flash to turn it into a flashlight.

入门Get started

若要开始使用此 API,请阅读 Xamarin.Essentials 的入门指南以确保在项目中正确安装和设置库。To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

若要访问 Flashlight 功能,需要以下特定于平台的设置。To access the Flashlight functionality the following platform specific setup is required.

需要具有 Flashlight 和 Camera 权限,并且必须在 Android 项目中进行配置。The Flashlight and Camera permissions are required and must be configured in the Android project. 可以通过以下方法添加权限:This can be added in the following ways:

打开 Properties 文件夹下的 AssemblyInfo.cs 文件并添加:Open the AssemblyInfo.cs file under the Properties folder and add:

[assembly: UsesPermission(Android.Manifest.Permission.Flashlight)]
[assembly: UsesPermission(Android.Manifest.Permission.Camera)]

或更新 Android 清单:OR Update Android Manifest:

打开 Properties 文件夹下的 AndroidManifest.xml 文件,并在“manifest”节点内添加以下代码。Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.

<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />

或右键单击 Android 项目并打开项目的属性。Or right click on the Android project and open the project's properties. 在“Android 清单”下找到“所需权限:”区域,然后选中“FLASHLIGHT”和“CAMERA”权限。Under Android Manifest find the Required permissions: area and check the FLASHLIGHT and CAMERA permissions. 这样会自动更新 AndroidManifest.xml 文件。This will automatically update the AndroidManifest.xml file.

通过添加这些权限,Google Play 将自动筛选出设备,而无需任何特定硬件。By adding these permissions Google Play will automatically filter out devices without specific hardware. 可以通过将以下代码添加到 Android 项目中的 AssemblyInfo.cs 文件中来绕过此操作:You can get around this by adding the following to your AssemblyInfo.cs file in your Android project:

[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]

使用 FlashlightUsing Flashlight

在你的类中添加对 Xamarin.Essentials 的引用:Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

可以通过 TurnOnAsyncTurnOffAsync 方法来打开或关闭手电筒:The flashlight can be turned on and off through the TurnOnAsync and TurnOffAsync methods:

try
{
    // Turn On
    await Flashlight.TurnOnAsync();

    // Turn Off
    await Flashlight.TurnOffAsync();
}
catch (FeatureNotSupportedException fnsEx)
{
    // Handle not supported on device exception
}
catch (PermissionException pEx)
{
    // Handle permission exception
}
catch (Exception ex)
{
    // Unable to turn on/off flashlight
}

平台实现细节Platform Implementation Specifics

Flashlight 类已根据设备的操作系统进行了优化。The Flashlight class has been optimized based on the device's operating system.

API 级别 23 及更高版本API Level 23 and Higher

在更新的 API 级别上,Torch 模式将用于打开或关闭设备的闪光单元。On newer API levels, Torch Mode will be used to turn on or off the flash unit of the device.

API 级别 22 及更高版本API Level 22 and Lower

创建一个相机表面纹理以打开或关闭相机单元的 FlashModeA camera surface texture is created to turn on or off the FlashMode of the camera unit.

APIAPI