Olá, neste post irei demonstrar como obter algumas informações do dispositivo em suas aplicações Xamarin.Forms.
Informações que podem ser obtidas
- Nome do dispositivo
- Id
- Fabricante
- Modelo
- Sistema Operacional
- Versão do Sistema Operacional
ADICIONANDO O NUGET PACKAGE
Clique com o botão direito em cima de sua Solution e selecione “Manage NuGet Packages for Solution…”.
Digite “Xam.Plugin.DeviceInfo” e selecione o plugin como demonstrado na imagem a seguir.
Selecione todos os projetos e clique no botão “Install”.
C#
Referencie o plugin DeviceInfo e utilize “CrossDeviceInfo.Current” seguido da propriedade desejada, como demonstrado a seguir.
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 Plugin.DeviceInfo; | |
using Xamarin.Forms; | |
namespace DemoDeviceInfo | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
DeviceName.Text = "DeviceName: " + CrossDeviceInfo.Current.DeviceName; | |
Id.Text = "Id: " + CrossDeviceInfo.Current.Id; | |
Manufacturer.Text = "Manufacturer: " + CrossDeviceInfo.Current.Manufacturer; | |
Model.Text = "Model: " + CrossDeviceInfo.Current.Model; | |
Version.Text = "Version: " + CrossDeviceInfo.Current.Version; | |
Platform.Text = "Platform: " + CrossDeviceInfo.Current.Platform.ToString(); | |
} | |
} | |
} |
XAML
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:DemoDeviceInfo" | |
x:Class="DemoDeviceInfo.MainPage"> | |
<StackLayout HorizontalOptions="Center" VerticalOptions="Center"> | |
<Label x:Name="DeviceName" /> | |
<Label x:Name="Id" /> | |
<Label x:Name="Manufacturer" /> | |
<Label x:Name="Model" /> | |
<Label x:Name="Platform" /> | |
<Label x:Name="Version" /> | |
</StackLayout> | |
</ContentPage> |
Resultado
Esse e todos os exemplos deste blog encontram-se disponíveis no GitHub.
Um comentário em “Informações do Dispositivo – Xamarin.Forms”