×
Namespaces

Variants
Actions
(Difference between revisions)

Como usar Cimbalino Windows Phone Toolkit Device Info - DeviceExtendedPropertiesService

From Nokia Developer Wiki
Jump to: navigation, search
saramgsilva (Talk | contribs)
(Saramgsilva -)
hamishwillee (Talk | contribs)
m (Hamishwillee - Strip out some of the exampole code comments in XML)
Line 48: Line 48:
  
 
<code csharp>
 
<code csharp>
/// <summary>
 
 
     /// This class contains static references to all the view models in the
 
     /// This class contains static references to all the view models in the
 
     /// application and provides an entry point for the bindings.
 
     /// application and provides an entry point for the bindings.
    /// </summary>
 
 
     public class ViewModelLocator
 
     public class ViewModelLocator
 
     {
 
     {
        /// <summary>
 
 
         /// Initializes a new instance of the ViewModelLocator class.
 
         /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
 
 
         public ViewModelLocator()
 
         public ViewModelLocator()
 
         {
 
         {
Line 89: Line 85:
  
 
<code csharp>
 
<code csharp>
  /// <summary>
 
 
     /// This class contains properties that the main View can data bind to.
 
     /// This class contains properties that the main View can data bind to.
    /// </summary>
 
 
     public class MainViewModel : ViewModelBase
 
     public class MainViewModel : ViewModelBase
 
     {
 
     {
        /// <summary>
 
 
         /// The device extended properties service
 
         /// The device extended properties service
        /// </summary>
 
 
         private readonly IDeviceExtendedPropertiesService _deviceExtendedPropertiesService;
 
         private readonly IDeviceExtendedPropertiesService _deviceExtendedPropertiesService;
  
        /// <summary>
 
 
         /// Initializes a new instance of the MainViewModel class.
 
         /// Initializes a new instance of the MainViewModel class.
        /// </summary>
 
 
         public MainViewModel(IDeviceExtendedPropertiesService deviceExtendedPropertiesService)
 
         public MainViewModel(IDeviceExtendedPropertiesService deviceExtendedPropertiesService)
 
         {
 
         {
Line 107: Line 97:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the device unique identifier.
 
         /// Gets the device unique identifier.
        /// </summary>
 
        /// <value>
 
        /// The device unique identifier.
 
        /// </value>
 
 
         public string DeviceUniqueID
 
         public string DeviceUniqueID
 
         {
 
         {
Line 121: Line 106:
 
         }
 
         }
  
        /// <summary>
+
 
 
         /// Gets the application current memory usage.
 
         /// Gets the application current memory usage.
        /// </summary>
 
        /// <value>
 
        /// The application current memory usage.
 
        /// </value>
 
 
         public long ApplicationCurrentMemoryUsage
 
         public long ApplicationCurrentMemoryUsage
 
         {
 
         {
Line 135: Line 116:
 
         }
 
         }
  
        /// <summary>
+
 
 
         /// Gets the application peak memory usage.
 
         /// Gets the application peak memory usage.
        /// </summary>
 
        /// <value>
 
        /// The application peak memory usage.
 
        /// </value>
 
 
         public long ApplicationPeakMemoryUsage
 
         public long ApplicationPeakMemoryUsage
 
         {
 
         {
Line 149: Line 126:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the device firmware version.
 
         /// Gets the device firmware version.
        /// </summary>
 
        /// <value>
 
        /// The device firmware version.
 
        /// </value>
 
 
         public string DeviceFirmwareVersion
 
         public string DeviceFirmwareVersion
 
         {
 
         {
Line 163: Line 135:
 
         }
 
         }
  
        /// <summary>
+
 
 
         /// Gets the device hardware version.
 
         /// Gets the device hardware version.
        /// </summary>
 
        /// <value>
 
        /// The device hardware version.
 
        /// </value>
 
 
         public string DeviceHardwareVersion
 
         public string DeviceHardwareVersion
 
         {
 
         {
Line 177: Line 145:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the device manufacturer.
 
         /// Gets the device manufacturer.
        /// </summary>
 
        /// <value>
 
        /// The device manufacturer.
 
        /// </value>
 
 
         public string DeviceManufacturer
 
         public string DeviceManufacturer
 
         {
 
         {
Line 191: Line 154:
 
         }
 
         }
  
        /// <summary>
+
 
 
         /// Gets the name of the device.
 
         /// Gets the name of the device.
        /// </summary>
 
        /// <value>
 
        /// The name of the device.
 
        /// </value>
 
 
         public string DeviceName
 
         public string DeviceName
 
         {
 
         {
Line 205: Line 164:
 
         }
 
         }
  
        /// <summary>
+
 
 
         /// Gets the device total memory.
 
         /// Gets the device total memory.
        /// </summary>
 
        /// <value>
 
        /// The device total memory.
 
        /// </value>
 
 
         public long DeviceTotalMemory
 
         public long DeviceTotalMemory
 
         {
 
         {
Line 219: Line 174:
 
         }
 
         }
  
        /// <summary>
+
 
 
         /// Gets the name of the original mobile operator.
 
         /// Gets the name of the original mobile operator.
        /// </summary>
 
        /// <value>
 
        /// The name of the original mobile operator.
 
        /// </value>
 
 
         public string OriginalMobileOperatorName
 
         public string OriginalMobileOperatorName
 
         {
 
         {
Line 237: Line 188:
 
== Implementando a View ==
 
== Implementando a View ==
  
A MainPage.xaml será algo do género:
+
A '''MainPage.xaml''' será algo do género:
  
 
<code xml>
 
<code xml>

Revision as of 02:21, 19 November 2013

Este artigo tem como objetivo mostrar como usar Cimbalino Windows Phone Toolkit Device Info– DeviceExtendedPropertiesService.

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata

Exemplo de código
Testado com
SDK: Windows Phone 8.0 SDK, Windows Phone 7.1.1 SDK
Aparelho(s): Nokia Lumia 800, Nokia Lumia 920

Compatibilidade
Platform Security
Capabilities: ID_CAP_IDENTITY_DEVICE

Artigo
Tradução:
Por saramgsilva
Última alteração feita por hamishwillee em 19 Nov 2013


Contents

Introdução

Cimbalino Windows Phone Toolkit é um conjunto de itens uteis e poderosos para ajudar na implementação de aplicações Windows Phone. Projeto base do toolkit contendo serviços para implementação do padrão de MVVM, conversores, classes auxiliares, métodos de extensões;

Cimbalino.Phone.Toolkit.DeviceInfo - Projeto do toolkit contendo serviços para implementação do padrão de MVVM com acesso a informação do dispositivo

Note.pngNote: é preciso definir a capacidade ID_CAP_IDENTITY_DEVICE no ficheiro manifest.

Contruindo o exemplo

O código fonte pode ser obtido em Exemplo DeviceExtendedPropertiesService(github).

Os pacotes estão disponíveis em Nuget Package Manager (para ambos os "targets") e podem ser instalados, para mais detalhes consultar: Como instalar os pacotes Cimbalino Windows Phone Toolkit. O exemplo deste artigo usa MVVM Light para ajudar na implementação do padrão MVVM, para mais detalhes consultar:

Registando os serviços

Devemos começar por registar cada serviço no ViewModelLocator, como podemos ver de seguida:

    /// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
public class ViewModelLocator
{
/// Initializes a new instance of the ViewModelLocator class.
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
Cimbalino.Phone.Toolkit.Services.DeviceExtendedPropertiesService
if (!SimpleIoc.Default.IsRegistered<IDeviceExtendedPropertiesService>())
{
SimpleIoc.Default.Register<IDeviceExtendedPropertiesService, DeviceExtendedPropertiesService>();
}
 
SimpleIoc.Default.Register<MainViewModel>();
}
 
public MainViewModel MainViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
 
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}

Implementando a ViewModel

Em seguinda devemos implementar o MainViewModel, como podemos ver de seguida:

    /// This class contains properties that the main View can data bind to.
public class MainViewModel : ViewModelBase
{
/// The device extended properties service
private readonly IDeviceExtendedPropertiesService _deviceExtendedPropertiesService;
 
/// Initializes a new instance of the MainViewModel class.
public MainViewModel(IDeviceExtendedPropertiesService deviceExtendedPropertiesService)
{
_deviceExtendedPropertiesService = deviceExtendedPropertiesService;
}
 
/// Gets the device unique identifier.
public string DeviceUniqueID
{
get
{
return Convert.ToBase64String(_deviceExtendedPropertiesService.DeviceUniqueId);
}
}
 
 
/// Gets the application current memory usage.
public long ApplicationCurrentMemoryUsage
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<long>("ApplicationCurrentMemoryUsage");
}
}
 
 
/// Gets the application peak memory usage.
public long ApplicationPeakMemoryUsage
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<long>("ApplicationPeakMemoryUsage");
}
}
 
/// Gets the device firmware version.
public string DeviceFirmwareVersion
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceFirmwareVersion");
}
}
 
 
/// Gets the device hardware version.
public string DeviceHardwareVersion
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceHardwareVersion");
}
}
 
/// Gets the device manufacturer.
public string DeviceManufacturer
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceManufacturer");
}
}
 
 
/// Gets the name of the device.
public string DeviceName
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceName");
}
}
 
 
/// Gets the device total memory.
public long DeviceTotalMemory
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<long>("DeviceTotalMemory");
}
}
 
 
/// Gets the name of the original mobile operator.
public string OriginalMobileOperatorName
{
get
{
return _deviceExtendedPropertiesService.GetDeviceProperty<string>("OriginalMobileOperatorName");
}
}
}

Implementando a View

A MainPage.xaml será algo do género:

<phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
DataContext="{Binding MainViewModel,
Source={StaticResource Locator}}"

SupportedOrientations="Portrait"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
 
<!-- LayoutRoot is the root grid where all page content is placed -->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
 
<!-- TitlePanel contains the name of the application and page title -->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Margin="12,0"
Style="{StaticResource PhoneTextTitle2Style}"
Text="Cimbalino Sample" />
</StackPanel>
 
<!-- ContentPanel - place additional content here -->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<TextBlock TextWrapping="Wrap">
Device Unique ID: <LineBreak/><Run Text="{Binding DeviceUniqueID}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,50,0,0">
Application Current Memory Usage: <LineBreak/><Run Text="{Binding ApplicationCurrentMemoryUsage}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,100,0,0">
Application Peak Memory Usage: <LineBreak/><Run Text="{Binding ApplicationPeakMemoryUsage}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,150,0,0">
Device Firmware Version: <LineBreak/><Run Text="{Binding DeviceFirmwareVersion}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,200,0,0">
Device Hardware Version: <LineBreak/><Run Text="{Binding DeviceHardwareVersion}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,250,0,0">
Device Manufacturer: <LineBreak/><Run Text="{Binding DeviceManufacturer}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,300,0,0">
Device Name: <LineBreak/><Run Text="{Binding DeviceName}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,350,0,0">
Device Total Memory: <LineBreak/><Run Text="{Binding DeviceTotalMemory}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,400,0,0">
Original Mobile Operator Name: <LineBreak/><Run Text="{Binding OriginalMobileOperatorName}" />
</TextBlock>
</Grid>
</Grid>
 
</phone:PhoneApplicationPage>
65 page views in the last 30 days.
×