This code example shows how to launch the phone application to make a call from your app using PhoneCallService from the Cimbalino Windows Phone Toolkit.

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 into 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.PhoneDialer contains MVVM compatible services for phone dialler access.

The Cimbalino Toolkit's "Phone Call" service is an MVVM compatible wrapper around the systemPhoneCallTask, that can be used to start the "Phone app". The kit provides both the IPhoneCallService interface and its implementation PhoneCallService is required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

Building the example code

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

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<IPhoneCallService>())
  10. {
  11. SimpleIoc.Default.Register<IPhoneCallService, PhoneCallService>();
  12. }
  13. SimpleIoc.Default.Register<MainViewModel>();
  14. }
  15. /// Gets the main view model.
  16. public MainViewModel MainViewModel
  17. {
  18. get
  19. {
  20. return ServiceLocator.Current.GetInstance<MainViewModel>();
  21. }
  22. }
  23. public static void Cleanup()
  24. {
  25. // TODO Clear the ViewModels
  26. var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];
  27. viewModelLocator.MainViewModel.Cleanup();
  28. }
  29. }
Implementing the ViewModel

Implement the MainViewModel (MainViewModel.cs) as shown below. The highlighted sections show the MainViewModel constructor taking the IPhoneCallService parameter and assign it to a private member. Later on the member is used to call Show() to launch thePhone app.
  1. using System.Windows;
  2. using System.Windows.Input;
  3. using System.Windows.Threading;
  4. using Cimbalino.Phone.Toolkit.Services;
  5. using GalaSoft.MvvmLight.Command;
  6. using GalaSoft.MvvmLight;
  7. /// This class contains properties that the main View can data bind to.
  8. public class MainViewModel : ViewModelBase
  9. {
  10. /// The phone call service
  11. private readonly IPhoneCallService _phoneCallService;
  12. /// The number
  13. private string _number;
  14. /// The name
  15. private string _name;
  16. /// Initializes a new instance of the MainViewModel class.
  17. public MainViewModel(IPhoneCallService phoneCallService)
  18. {
  19. _phoneCallService = phoneCallService;
  20. CallCommand =new RelayCommand(CallTo);
  21. }
  22. /// Gets or sets the number.
  23. public string Number
  24. {
  25. get
  26. {
  27. return _number;
  28. }
  29. set
  30. {
  31. Set("Number", ref _number, value);
  32. }
  33. }
  34. /// Gets or sets the name.
  35. public string Name
  36. {
  37. get
  38. {
  39. return _name;
  40. }
  41. set
  42. {
  43. Set("Name", ref _name, value);
  44. }
  45. }
  46. /// Gets the call command.
  47. public ICommand CallCommand { get; private set; }
  48. /// Calls to.
  49. private void CallTo()
  50. {
  51. _phoneCallService.Show(Number, Name);
  52. }
  53. }
Implementing the View

The rest of the app is "plumbing" to hook up the ViewModels to the View and to send commands from the UI to the invoke the service.

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:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  7. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  8. DataContext="{Binding MainViewModel,
  9. Source={StaticResource Locator}}"
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11. FontSize="{StaticResource PhoneFontSizeNormal}"
  12. Foreground="{StaticResource PhoneForegroundBrush}"
  13. Orientation="Portrait"
  14. SupportedOrientations="Portrait"
  15. shell:SystemTray.IsVisible="True"
  16. mc:Ignorable="d">
  17. <!-- LayoutRoot is the root grid where all page content is placed -->
  18. <Grid x:Name="LayoutRoot" Background="Transparent">
  19. <Grid.RowDefinitions>
  20. <RowDefinition Height="Auto" />
  21. <RowDefinition Height="*" />
  22. </Grid.RowDefinitions>
  23. <!-- TitlePanel contains the name of the application and page title -->
  24. <StackPanel x:Name="TitlePanel"
  25. Grid.Row="0"
  26. Margin="12,17,0,28">
  27. <TextBlock Margin="12,0"
  28. Style="{StaticResource PhoneTextTitle2Style}"
  29. Text="Cimbalino Sample" />
  30. <TextBlock Margin="9,-7,0,0"
  31. Style="{StaticResource PhoneTextTitle1Style}"
  32. Text="PhoneDailer" />
  33. </StackPanel>
  34. <!-- ContentPanel - place additional content here -->
  35. <Grid x:Name="ContentPanel"
  36. Grid.Row="1"
  37. Margin="12,0,12,0">
  38. <TextBlock TextWrapping="Wrap">
  39. Name:
  40. </TextBlock>
  41. <TextBlock Margin="0,100,0,-100" TextWrapping="Wrap">
  42. Number:
  43. </TextBlock>
  44. <TextBox Text="{Binding Name,Mode=TwoWay}" Margin="-10,20,10,496"/>
  45. <TextBox Text="{Binding Number,Mode=TwoWay}" Margin="-10,122,10,399"/>
  46. <Button Margin="0,219,0,293"
  47. Command="{Binding CallCommand}"
  48. Content="Call" />
  49. </Grid>
  50. </Grid>
  51. </phone:PhoneApplicationPage>