Olá, neste post irei demonstrar como sua aplicação Xamarin.Forms pode acionar o modo vibrar do dispositivo.
ADICIONANDO O NUGET PACKAGE
Clique com o botão direito em cima de sua Solution e selecione “Manage NuGet Packages for Solution…”.
Digite “Xam.Plugins.Vibrate” e selecione o plugin como demonstrado na imagem a seguir.
Selecione todos os projetos e clique no botão “Install”.
Xaml
Crie um Button para chamar o método ButtonClick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:local="clr-namespace:DemoVibrate" | |
x:Class="DemoVibrate.MainPage"> | |
<StackLayout VerticalOptions="Center" | |
HorizontalOptions="Center"> | |
<Button Text="Vibrate" Clicked="ButtonClick"/> | |
</StackLayout> | |
</ContentPage> |
C#
No método ButtonClick utilize CrossVibrate.Current.Vibration e passe como parâmetro um TimeSpan com o tempo que deseja que o dispositivo vibre.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Plugin.Vibrate; | |
using Xamarin.Forms; | |
namespace DemoVibrate | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
} | |
public void ButtonClick(object sender, EventArgs e) | |
{ | |
CrossVibrate.Current.Vibration(TimeSpan.FromSeconds(1)); | |
} | |
} | |
} |
iOS
Não há API para vibrar por um período de tempo específico, portanto, ele irá vibrar durante o tempo padrão especificado pelo sistema (cerca de 500 milissegundos).
Esse e todos os exemplos deste blog encontram-se disponíveis no GitHub.