Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to use Cimballino Windows Phone Toolkit PhoneDialer - PhoneCallService

From Wiki
Jump to: navigation, search

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.

WP Metro Icon MobilePhone.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code Example
Source file: PhoneCallService Sample (github)
Tested with
SDK: Windows Phone 8.0 SDK, Windows Phone 7.1.1. SDK
Devices(s): Lumia 920, Lumia 800
CompatibilityPlatform Security
Capabilities: ID_CAP_PHONEDIALER
Article
Created: saramgsilva (27 Nov 2013)
Last edited: saramgsilva (04 Dec 2013)

Contents

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 which deliver different features, ranging from base MVVM services and helpers, through to code for background agents, and for accessing media library, location services etc. 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 system PhoneCallTask, which can be used to start the "Phone app". The kit provides both the IPhoneCallService interface and its implementation PhoneCallService required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

This article explains how to use the service in an MVVM Light app. Developers should first read How to use MVVM Light Toolkit for Windows Phone for a general overview of MVVM Light and how to use services.

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. For more details see:


The sample is built using MVVM Light and the article does not go into great detail about the plumbing/infrastructure provided by the toolkit. For more information see:


Note.pngNote: 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.  
  16.         /// Gets the main view model.
  17.         public MainViewModel MainViewModel
  18.         {
  19.             get
  20.             {
  21.                 return ServiceLocator.Current.GetInstance<MainViewModel>();
  22.             }
  23.         }
  24.  
  25.         public static void Cleanup()
  26.         {
  27.             // TODO Clear the ViewModels
  28.             var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];
  29.             viewModelLocator.MainViewModel.Cleanup();
  30.         }
  31.     }


Implementing the ViewModel

Implement the MainViewModel (MainViewModel.cs) as shown below. The highlighted sections show the MainViewModel constructor taking the IPhoneCallService parameter and assigning it to a private member. Later on the member is used to call Show() in order to launch the Phone app.

  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 phone call service
  14.         private readonly IPhoneCallService _phoneCallService;
  15.  
  16.         /// The number
  17.         private string _number;
  18.  
  19.         /// The name
  20.         private string _name;
  21.  
  22.         /// Initializes a new instance of the MainViewModel class.
  23.         public MainViewModel(IPhoneCallService phoneCallService)
  24.         {
  25.             _phoneCallService = phoneCallService;
  26.             CallCommand =new RelayCommand(CallTo);
  27.         }
  28.  
  29.         /// Gets or sets the number.
  30.         public string Number
  31.         {
  32.             get
  33.             {
  34.                 return _number;
  35.             }
  36.             set
  37.             {
  38.                 Set("Number", ref _number, value);
  39.             }
  40.         }
  41.  
  42.         /// Gets or sets the name.
  43.         public string Name
  44.         {
  45.             get
  46.             {
  47.                 return _name;
  48.             }
  49.             set
  50.             {
  51.                 Set("Name", ref _name, value);
  52.             }
  53.         }
  54.  
  55.         /// Gets the call command.
  56.         public ICommand CallCommand { get; private set; }
  57.  
  58.         /// Calls to.
  59.         private void CallTo()
  60.         {
  61.             _phoneCallService.Show(Number, Name);
  62.         }
  63.     }

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. How to use MVVM Light Toolkit for Windows Phone explains most of what is going on.

The MainPage.xaml is as shown below. The highlighted section shows how to bind the MainViewModel to the UI:

  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.  
  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 PhoneTextTitle1Style}"
  34.                        Text="PhoneDailer" />
  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">
  42.                 Name:
  43.             </TextBlock>
  44.             <TextBlock Margin="0,100,0,-100" TextWrapping="Wrap">
  45.                 Number:
  46.             </TextBlock>
  47.             <TextBox Text="{Binding Name,Mode=TwoWay}" Margin="-10,20,10,496"/>
  48.             <TextBox Text="{Binding Number,Mode=TwoWay}" Margin="-10,122,10,399"/>
  49.             <Button Margin="0,219,0,293"
  50.                     Command="{Binding CallCommand}"
  51.                     Content="Call" />
  52.         </Grid>
  53.     </Grid>
  54. </phone:PhoneApplicationPage>
This page was last modified on 4 December 2013, at 12:55.
224 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×