How to Use Cimbalino Windows Phone Toolkit - Location

Introduction

The Cimbalino Windows Phone Toolkit delivers a set of useful and powerful MVVM-compatible tools and services to help developers build Silverlight applications for Windows Phone. The Toolkit is divided in projects that deliver various features, ranging from base MVVM services and helpers, through to code for background agents and for accessing media library, location services and so on. Cimbalino.Phone.Toolkit.Location is a MVVM compatible services for location access.

ILocationService represents an interface for a service capable of handling the device location capabilities. The implementation is: LocationService.

Building the example code

The source code for the code example is available here: Location Sample (Github).

To build the source code you will also need the MVVM Light Toolkit and the Cimbalino Windows Phone Toolkit. Their packages are available in the Nuget Package Manager.

Note: you must specify the following capabilities in the app manifest: ID_CAP_LOCATION.

Registering the service

Register the service in the ViewModelLocator constructor as shown below:

  1. /// This class contains static references to all the view models in the  
  2. /// application and provides an entry point for the bindings.  
  3. public class ViewModelLocator  
  4. {  
  5.     /// Initializes a new instance of the ViewModelLocator class.  
  6.     public ViewModelLocator()  
  7.     {  
  8.         ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  9.         if (!SimpleIoc.Default.IsRegistered<ILocationService>())  
  10.         {  
  11.             SimpleIoc.Default.Register<ILocationService, LocationService>();  
  12.         }  
  13.         SimpleIoc.Default.Register<MainViewModel>();  
  14.     }  
  15.   
  16.   
  17.     /// Gets the main view model.  
  18.     public MainViewModel MainViewModel  
  19.     {  
  20.         get  
  21.         {  
  22.             return ServiceLocator.Current.GetInstance<MainViewModel>();  
  23.         }  
  24.     }  
  25.   
  26.     public static void Cleanup()  
  27.     {  
  28.         // TODO Clear the ViewModels  
  29.         var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];  
  30.         viewModelLocator.MainViewModel.Cleanup();  
  31.     }  
  32. }  
Implementing the ViewModel

Then we should implement the MainViewModel class as in the following:
  1. using System.Windows;  
  2. using System.Windows.Input;  
  3. using System.Windows.Threading;  
  4.   
  5. using Cimbalino.Phone.Toolkit.Services;  
  6.   
  7. using GalaSoft.MvvmLight.Command;  
  8. using GalaSoft.MvvmLight;  
  9.   
  10. /// This class contains properties that the main View can data bind to.  
  11. public class MainViewModel : ViewModelBase  
  12. {  
  13.     /// The location service  
  14.     private readonly ILocationService _locationService;  
  15.   
  16.     private bool _isLocationEnable;  
  17.   
  18.     /// Define if is start enable  
  19.     private bool _isStartEnable;  
  20.   
  21.     /// Define if is stop enable  
  22.     private bool _isStopEnable;  
  23.   
  24.     /// The latitude  
  25.     private double _latitude;  
  26.   
  27.     /// The logitude  
  28.     private double _longitude;  
  29.   
  30.     /// The status  
  31.     private LocationServiceStatus _status;  
  32.   
  33.     /// Initializes a new instance of the MainViewModel class.  
  34.     public MainViewModel(ILocationService locationService)  
  35.     {  
  36.         IsStartEnable = true;  
  37.         IsLocationEnable = true;  
  38.         IsStopEnable = false;  
  39.         _locationService = locationService;  
  40.         _locationService.ReportInterval = 5;  
  41.         _locationService.PositionChanged += LocationService_PositionChanged;  
  42.         _locationService.StatusChanged += LocationService_StatusChanged;  
  43.         StartCommand =new RelayCommand(Start);  
  44.         StopCommand =new RelayCommand(Stop);  
  45.         LocationCommand = new RelayCommand(GetLocation);  
  46.     }  
  47.   
  48.     /// Gets or sets a value indicating whether [is location enable].  
  49.     public bool IsLocationEnable  
  50.     {  
  51.         get  
  52.         {  
  53.             return this._isLocationEnable;  
  54.         }  
  55.         set  
  56.         {  
  57.             Set("IsLocationEnable"ref _isLocationEnable, value);  
  58.         }  
  59.     }  
  60.   
  61.   
  62.     /// Gets or sets a value indicating whether [is start enable].  
  63.     public bool IsStartEnable  
  64.     {  
  65.         get  
  66.         {  
  67.             return this._isStartEnable;  
  68.         }  
  69.         set  
  70.         {  
  71.             Set("IsStartEnable"ref _isStartEnable, value);  
  72.         }  
  73.     }  
  74.   
  75.     /// Gets or sets a value indicating whether [is stop enable].  
  76.     public bool IsStopEnable  
  77.     {  
  78.         get  
  79.         {  
  80.             return this._isStopEnable;  
  81.         }  
  82.         set  
  83.         {  
  84.             Set("IsStopEnable"ref _isStopEnable, value);  
  85.         }  
  86.     }  
  87.   
  88.     /// Gets or sets the latitude.  
  89.     public double Latitude  
  90.     {  
  91.         get  
  92.         {  
  93.             return _latitude;  
  94.         }  
  95.         set  
  96.         {  
  97.             Set("Latitude"ref _latitude, value);  
  98.         }  
  99.     }  
  100.   
  101.     /// Gets or sets the location command.  
  102.     public ICommand LocationCommand { getprivate set; }  
  103.   
  104.     /// Gets or sets the longitude.  
  105.     public double Longitude  
  106.     {  
  107.         get  
  108.         {  
  109.             return this._longitude;  
  110.         }  
  111.         set  
  112.         {  
  113.             Set("Longitude"ref _longitude, value);  
  114.         }  
  115.     }  
  116.   
  117.     /// Gets or sets the start command.  
  118.     public ICommand StartCommand { getprivate set; }  
  119.   
  120.     /// Gets or sets the status.  
  121.     public LocationServiceStatus Status  
  122.     {  
  123.         get  
  124.         {  
  125.             return _status;  
  126.         }  
  127.         set  
  128.         {  
  129.             Set("Status"ref _status, value);  
  130.         }  
  131.     }  
  132.   
  133.     /// Gets or sets the stop command.  
  134.     public ICommand StopCommand { getprivate set; }  
  135.   
  136.     /// Unregisters this instance from the Messenger class.  
  137.     public override void Cleanup()  
  138.     {  
  139.         base.Cleanup();  
  140.         _locationService.PositionChanged -= LocationService_PositionChanged;  
  141.         _locationService.StatusChanged -= LocationService_StatusChanged;  
  142.     }  
  143.   
  144.     /// Gets the location.  
  145.     private async void GetLocation()  
  146.     {  
  147.         var result = await _locationService.GetPositionAsync();  
  148.         Longitude = result.Longitude;  
  149.         Latitude = result.Latitude;  
  150.     }  
  151.   
  152.     /// Handles the PositionChanged event of the LocationService control.  
  153.     private void LocationService_PositionChanged(object sender, LocationServicePositionChangedEventArgs e)  
  154.     {  
  155.         Deployment.Current.Dispatcher.BeginInvoke(delegate  
  156.             {  
  157.                 Latitude = e.Position.Latitude;  
  158.                 Longitude = e.Position.Longitude;  
  159.             });  
  160.     }  
  161.   
  162.     /// Handles the StatusChanged event of the _locationService control.  
  163.     private void LocationService_StatusChanged(object sender, LocationServiceStatusChangedEventArgs e)  
  164.     {  
  165.         Deployment.Current.Dispatcher.BeginInvoke(delegate { Status = e.Status; });  
  166.     }  
  167.   
  168.     /// Starts the location service.  
  169.     private void Start()  
  170.     {  
  171.         IsStartEnable = false;  
  172.         IsStopEnable = true;  
  173.         IsLocationEnable = false;  
  174.         _locationService.Start();  
  175.     }  
  176.   
  177.     /// Stops the location service.  
  178.     private void Stop()  
  179.     {  
  180.         IsLocationEnable = true;  
  181.         IsStartEnable = true;  
  182.         IsStopEnable = false;  
  183.         _locationService.Stop();  
  184.     }  
  185. }  
Implementing the View

Add the binding in the main page is like:

 

  1. DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"  
  2.   
  3. The MainPage.xaml can be the following:  
  4. <phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"  
  5.                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  6.                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  7.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  10.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  11.                             DataContext="{Binding MainViewModel,  
  12.                                                   Source={StaticResource Locator}}"  
  13.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  14.                             FontSize="{StaticResource PhoneFontSizeNormal}"  
  15.                             Foreground="{StaticResource PhoneForegroundBrush}"  
  16.                             Orientation="Portrait"  
  17.                             SupportedOrientations="Portrait"  
  18.                             shell:SystemTray.IsVisible="True"  
  19.                             mc:Ignorable="d">  
  20.    
  21.     <!--  LayoutRoot is the root grid where all page content is placed  -->  
  22.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  23.         <Grid.RowDefinitions>  
  24.             <RowDefinition Height="Auto" />  
  25.             <RowDefinition Height="*" />  
  26.         </Grid.RowDefinitions>  
  27.    
  28.         <!--  TitlePanel contains the name of the application and page title  -->  
  29.         <StackPanel x:Name="TitlePanel"  
  30.                     Grid.Row="0"  
  31.                     Margin="12,17,0,28">  
  32.             <TextBlock Margin="12,0"  
  33.                        Style="{StaticResource PhoneTextTitle2Style}"  
  34.                        Text="Cimbalino Sample" />  
  35.             <TextBlock Margin="9,-7,0,0"  
  36.                        Style="{StaticResource PhoneTextTitle1Style}"  
  37.                        Text="Location" />  
  38.         </StackPanel>  
  39.    
  40.         <!--  ContentPanel - place additional content here  -->  
  41.         <Grid x:Name="ContentPanel"  
  42.               Grid.Row="1"  
  43.               Margin="12,0,12,0">  
  44.             <TextBlock TextWrapping="Wrap">  
  45.                 Latitude:<Run Text="{Binding Latitude}" />  
  46.             </TextBlock>  
  47.             <TextBlock Margin="0,51,0,-51" TextWrapping="Wrap">  
  48.                 Logitude:<Run Text="{Binding Longitude}" />  
  49.             </TextBlock>  
  50.             <TextBlock Margin="0,102,0,-102" TextWrapping="Wrap">  
  51.                 Status:<Run Text="{Binding Status}" />  
  52.             </TextBlock>  
  53.             <Button Margin="0,298,0,214"  
  54.                     IsEnabled="{Binding IsStartEnable}"  
  55.                     Command="{Binding StartCommand}"  
  56.                     Content="Start" />  
  57.             <Button Height="76"  
  58.                     IsEnabled="{Binding IsStopEnable}"  
  59.                     Margin="0,0,0,138"  
  60.                     VerticalAlignment="Bottom"  
  61.                     Command="{Binding StopCommand}"  
  62.                     Content="Stop" />  
  63.             <Button Margin="0,219,0,293"  
  64.                     IsEnabled="{Binding IsLocationEnable}"  
  65.                     Command="{Binding LocationCommand}"  
  66.                     Content="Get location" />  
  67.         </Grid>  
  68.     </Grid>  
  69. </phone:PhoneApplicationPage>  


Similar Articles