Xamarin.Essentials: VibraciónXamarin.Essentials: Vibration
La clase Vibration permite iniciar y detener la funcionalidad de vibración durante un período de tiempo determinado.The Vibration class lets you start and stop the vibrate functionality for a desired amount of time.
Primeros pasosGet started
Para empezar a usar esta API, lea la guía de introducción para Xamarin.Essentials con el fin de asegurarse de que la biblioteca está correctamente instalada y configurada en los proyectos.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.
Para acceder a la funcionalidad de Vibration, se requiere la siguiente configuración específica para la plataforma.To access the Vibration functionality the following platform specific setup is required.
El permiso Vibrate (Vibrar) es necesario y se debe configurar en el proyecto de Android.The Vibrate permission is required and must be configured in the Android project. Se puede agregar de las siguientes maneras:This can be added in the following ways:
Abra el archivo AssemblyInfo.cs de la carpeta Propiedades y agregue lo siguiente:Open the AssemblyInfo.cs file under the Properties folder and add:
[assembly: UsesPermission(Android.Manifest.Permission.Vibrate)]
O BIEN, actualice el manifiesto de Android:OR Update Android Manifest:
Abra el archivo AndroidManifest.xml de la carpeta Propiedades y agregue lo siguiente dentro del nodo 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.VIBRATE" />
O haga clic con el botón derecho en el proyecto de Android y abra las propiedades del proyecto.Or right click on the Android project and open the project's properties. En Manifiesto de Android, busque el área Permisos requeridos: y active el permiso VIBRATE (Vibrar).Under Android Manifest find the Required permissions: area and check the VIBRATE permission. Esto actualizará automáticamente el archivo AndroidManifest.xml.This will automatically update the AndroidManifest.xml file.
Uso de VibrationUsing Vibration
Agregue una referencia a Xamarin.Essentials en la clase:Add a reference to Xamarin.Essentials in your class:
using Xamarin.Essentials;
La funcionalidad de Vibration se puede solicitar para un período de tiempo concreto o el valor predeterminado de 500 milisegundos.The Vibration functionality can be requested for a set amount of time or the default of 500 milliseconds.
try
{
// Use default vibration length
Vibration.Vibrate();
// Or use specified time
var duration = TimeSpan.FromSeconds(1);
Vibration.Vibrate(duration);
}
catch (FeatureNotSupportedException ex)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Other error has occurred.
}
Se puede solicitar la cancelación de la vibración del dispositivo con el método Cancel
:Cancellation of device vibration can be requested with the Cancel
method:
try
{
Vibration.Cancel();
}
catch (FeatureNotSupportedException ex)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Other error has occurred.
}
Diferencias entre plataformasPlatform Differences
No hay diferencias entre las plataformas.No platform differences.
APIAPI
- Código fuente de VibrationVibration source code
- Documentación de API para VibrationVibration API documentation
Vídeo relacionadoRelated Video
Encuentre más vídeos de Xamarin en Channel 9 y YouTube.Find more Xamarin videos on Channel 9 and YouTube.