How to Use Cimbalino Windows Phone Toolkit Media Library - FMRadioService

The purpose of this sample is to show how to use the Cimbalino Windows Phone Toolkit Media Library, FMRadioService.

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 the media library, location services and so on. Cimbalino.Phone.Toolkit.MediaLibrary is a MVVM compatible services for media library access.

IFMRadioService represents a service capable of handling the device FM radio. The implementation is FMRadioService.

Building the example code

The source code for the code example is available here: (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_MEDIALIB.

Registering the service

Register the service in the ViewModelLocator constructor as highlighted below:

  1. using Cimbalino.Phone.Toolkit.Services;  
  2.  using GalaSoft.MvvmLight;  
  3.  using GalaSoft.MvvmLight.Ioc;  
  4.  using Microsoft.Practices.ServiceLocation;  
  5.    
  6.  /// <summary>  
  7.  /// This class contains static references to all the view models in the  
  8.  /// application and provides an entry point for the bindings.  
  9.  /// </summary>  
  10.  public class ViewModelLocator  
  11.  {  
  12.      /// <summary>  
  13.      /// Initializes a new instance of the ViewModelLocator class.  
  14.      /// </summary>  
  15.      public ViewModelLocator()  
  16.      {  
  17.          ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  18.          if (!SimpleIoc.Default.IsRegistered<IFMRadioService>())  
  19.          {  
  20.              SimpleIoc.Default.Register<IFMRadioService, FMRadioService>();  
  21.          }  
  22.          SimpleIoc.Default.Register<MainViewModel>();  
  23.      }  
  24.    
  25.      /// <summary>  
  26.      /// Gets the main view model.  
  27.      /// </summary>  
  28.      /// <value>  
  29.      /// The main view model.  
  30.      /// </value>  
  31.      public MainViewModel MainViewModel  
  32.      {  
  33.          get  
  34.          {  
  35.              return ServiceLocator.Current.GetInstance<MainViewModel>();  
  36.          }  
  37.      }  
  38.    
  39.      public static void Cleanup()  
  40.      {  
  41.          // TODO Clear the ViewModels  
  42.          var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];  
  43.          viewModelLocator.MainViewModel.Cleanup();  
  44.      }  
  45.  }  
Implementing the ViewModel

Then we should implement the MainViewModel 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.    using Microsoft.Devices.Radio;  
  11.    
  12.    /// <summary>  
  13.    /// This class contains properties that the main View can data bind to.  
  14.    /// </summary>  
  15.    public class MainViewModel : ViewModelBase  
  16.    {  
  17.        /// <summary>  
  18.        /// The FM radio service  
  19.        /// </summary>  
  20.        private readonly IFMRadioService _fmRadioService;  
  21.    
  22.        /// <summary>  
  23.        /// The frequency  
  24.        /// </summary>  
  25.        private double _frequency;  
  26.    
  27.        /// <summary>  
  28.        /// Initializes a new instance of the MainViewModel class.  
  29.        /// </summary>  
  30.        public MainViewModel(IFMRadioService fmRadioService)  
  31.        {  
  32.            _fmRadioService = fmRadioService;  
  33.            PropertyChanged += MainViewModel_PropertyChanged;  
  34.            Frequency = 91.7;  
  35.            _fmRadioService.CurrentRegion = RadioRegion.Europe;  
  36.            _fmRadioService.PowerOff();  
  37.            StopCommand= new RelayCommand(Stop);  
  38.            PlayCommand = new RelayCommand(Play);  
  39.        }  
  40.    
  41.        /// <summary>  
  42.        /// Plays this instance.  
  43.        /// </summary>  
  44.        private void Play()  
  45.        {  
  46.          _fmRadioService.PowerOn();  
  47.        }  
  48.    
  49.        /// <summary>  
  50.        /// Stops this instance.  
  51.        /// </summary>  
  52.        private void Stop()  
  53.        {  
  54.         _fmRadioService.PowerOff();    
  55.        }  
  56.    
  57.        /// <summary>  
  58.        /// Gets or sets the frequency.  
  59.        /// </summary>  
  60.        /// <value>  
  61.        /// The frequency.  
  62.        /// </value>  
  63.        public double Frequency  
  64.        {  
  65.            get  
  66.            {  
  67.                return this._frequency;  
  68.            }  
  69.            set  
  70.            {  
  71.                Set("Frequency"ref _frequency, value);  
  72.            }  
  73.        }  
  74.    
  75.        /// <summary>  
  76.        /// Gets the play command.  
  77.        /// </summary>  
  78.        /// <value>  
  79.        /// The play command.  
  80.        /// </value>  
  81.        public ICommand PlayCommand { getprivate set; }  
  82.    
  83.        /// <summary>  
  84.        /// Gets the stop command.  
  85.        /// </summary>  
  86.        /// <value>  
  87.        /// The stop command.  
  88.        /// </value>  
  89.        public ICommand StopCommand { getprivate set; }  
  90.    
  91.        /// <summary>  
  92.        /// Handles the PropertyChanged event of the MainViewModel control.  
  93.        /// </summary>  
  94.        /// <param name="sender">The source of the event.</param>  
  95.        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>  
  96.        private void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)  
  97.        {  
  98.            if (e.PropertyName == "Frequency")  
  99.            {  
  100.                _fmRadioService.Frequency = Frequency;  
  101.            }  
  102.        }  
  103.    }  
Implementing the View

The MainPage.xaml is as shown below.
  1. <phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"  
  2.                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.                             xmlns:converters="clr-namespace:Cimbalino.Phone.Toolkit.Converters;assembly=Cimbalino.Phone.Toolkit"  
  5.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  7.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  8.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  9.                             DataContext="{Binding MainViewModel,  
  10.                                                   Source={StaticResource Locator}}"  
  11.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  12.                             FontSize="{StaticResource PhoneFontSizeNormal}"  
  13.                             Foreground="{StaticResource PhoneForegroundBrush}"  
  14.                             Orientation="Portrait"  
  15.                             SupportedOrientations="Portrait"  
  16.                             shell:SystemTray.IsVisible="True"  
  17.                             mc:Ignorable="d">  
  18.     <!--  LayoutRoot is the root grid where all page content is placed  -->  
  19.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  20.         <Grid.RowDefinitions>  
  21.             <RowDefinition Height="Auto" />  
  22.             <RowDefinition Height="*" />  
  23.         </Grid.RowDefinitions>  
  24.    
  25.         <!--  TitlePanel contains the name of the application and page title  -->  
  26.         <StackPanel x:Name="TitlePanel"  
  27.                     Grid.Row="0"  
  28.                     Margin="12,17,0,28">  
  29.             <TextBlock Margin="12,0"  
  30.                        Style="{StaticResource PhoneTextTitle2Style}"  
  31.                        Text="Cimbalino Sample" />  
  32.             <TextBlock Margin="9,-7,0,0"  
  33.                        Style="{StaticResource PhoneTextTitle2Style}"  
  34.                        Text="FMRadioService" />  
  35.         </StackPanel>  
  36.    
  37.         <!--  ContentPanel - place additional content here  -->  
  38.         <Grid x:Name="ContentPanel"  
  39.               Grid.Row="1"  
  40.               Margin="12,0,12,0">  
  41.             <TextBlock TextWrapping="Wrap">This samples has the goal to show how to use Cimbalino Windows Phone Toolkit Media Library - FMRadioService</TextBlock>  
  42.    
  43.             <TextBlock Margin="0,90,0,-90" Text="Frequency:" />  
  44.             <TextBox Margin="0,122,0,451" Text="{Binding Frequency, Mode=TwoWay}" />  
  45.             <Button Margin="0,219,0,293"  
  46.                     Command="{Binding PlayCommand}"  
  47.                     Content="Play" />  
  48.             <Button Margin="0,351,0,161"  
  49.                     Command="{Binding StopCommand}"  
  50.                     Content="Stop" />  
  51.         </Grid>  
  52.     </Grid>  
  53. </phone:PhoneApplicationPage>