How to Implement Link Sharing Using 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 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.

The Cimbalino Toolkit's "Share Link" service is an MVVM compatible wrapper around the system ShareLinkTask, that can be used to launch a dialog to share links via social networks configured on the device. The kit provides both the IShareLinkService interface and its implementation ShareLinkServicerequired to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

showing the sharing button
Screenshot of the example app showing the sharing button.

Building the example code

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

Registering the service

Register the service in the ViewModelLocator constructor as shown below.

  1.  /// <summary>  
  2. /// Initializes a new instance of the ViewModelLocator class.  
  3. /// </summary>  
  4. public ViewModelLocator()  
  5. {  
  6.     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  7.   
  8.     if (!SimpleIoc.Default.IsRegistered<IShareLinkService>())  
  9.     {  
  10.         SimpleIoc.Default.Register<IShareLinkService, ShareLinkService>();  
  11.     }  
  12.   
  13.     SimpleIoc.Default.Register<MainViewModel>();  
  14. }  
  15.   
  16. public MainViewModel MainViewModel  
  17. {  
  18.     get  
  19.     {  
  20.         return ServiceLocator.Current.GetInstance<MainViewModel>();  
  21.     }  
  22. }  
  23.   
  24. public static void Cleanup()  
  25. {  
  26.     // TODO Clear the ViewModels  
  27. }  
Implementing the ViewModel

Implement the MainViewModel as shown below. The highlighted sections show the MainViewModel constructor taking the IShareLinkServiceparameter and assigning it to the private variable. Later on the private member is used to show the link service.
  1. public class MainViewModel : ViewModelBase  
  2. {  
  3.     /// <summary>  
  4.     /// The share link service.  
  5.     /// </summary>  
  6.     private readonly IShareLinkService _shareLinkService;  
  7.   
  8.     /// <summary>  
  9.     /// The public application url.  
  10.     /// </summary>  
  11.     private readonly string _appUrl;  
  12.   
  13.     /// <summary>  
  14.     /// Initializes a new instance of the <see cref="MainViewModel"/> class.  
  15.     /// </summary>  
  16.     /// <param name="shareLinkService">  
  17.     /// The share Link Service.  
  18.     /// </param>  
  19.     public MainViewModel(IShareLinkService shareLinkService)  
  20.     {  
  21.         _shareLinkService = shareLinkService;  
  22.   
  23.         ShareSocialNetworkCommand = new RelayCommand(ShareSocialNetwork);  
  24.         _appUrl = string.Concat("http://windowsphone.com/s?appid=8df00038-1b7a-406b-b33f-37a78b17348c");  
  25.     }  
  26.   
  27.     /// <summary>  
  28.     /// Gets the share social network command.  
  29.     /// </summary>  
  30.     public ICommand ShareSocialNetworkCommand { getprivate set; }  
  31.   
  32.     /// <summary>  
  33.     /// The share social network.  
  34.     /// </summary>  
  35.     private void ShareSocialNetwork()  
  36.     {  
  37.         const string Message = "This application is amazing, should try it! See in";  
  38.         _shareLinkService.Show("Cimbalino Toolkit Sample", Message, new Uri(_appUrl, UriKind.Absolute));  
  39.     }  
  40. }  
Implementing the view

The rest of the app is "plumbing" to hook up the MainViewModel to the View and send commands back to the invoke the service.

Add the binding in the main page is as in the following:

 

  1. DataContext="{Binding MainViewModel,  
  2.                      Source={StaticResource Locator}}"  
The MainPage.xaml can be the following:
  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:cimbalino="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"  
  5.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6.                             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
  7.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  9.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  10.                             DataContext="{Binding MainViewModel,  
  11.                                                   Source={StaticResource Locator}}"  
  12.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  13.                             FontSize="{StaticResource PhoneFontSizeNormal}"  
  14.                             Foreground="{StaticResource PhoneForegroundBrush}"  
  15.                             Orientation="Portrait"  
  16.                             SupportedOrientations="Portrait"  
  17.                             shell:SystemTray.IsVisible="True"  
  18.                             mc:Ignorable="d">  
  19.    
  20.     <!--  LayoutRoot is the root grid where all page content is placed  -->  
  21.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  22.         <Grid.RowDefinitions>  
  23.             <RowDefinition Height="Auto" />  
  24.             <RowDefinition Height="*" />  
  25.         </Grid.RowDefinitions>  
  26.    
  27.         <!--  TitlePanel contains the name of the application and page title  -->  
  28.         <StackPanel x:Name="TitlePanel"  
  29.                     Grid.Row="1"  
  30.                     Margin="0,5,12,396">  
  31.             <TextBlock Margin="12,0"  
  32.                        Style="{StaticResource PhoneTextTitle2Style}"  
  33.                        Text="Cimbalino Toolkit Sample" />  
  34.         </StackPanel>  
  35.         <TextBlock Grid.RowSpan="2"  
  36.                    Margin="12,50,-3,487"  
  37.                    Style="{StaticResource PhoneTextTitle3Style}"  
  38.                    TextWrapping="Wrap">  
  39.             This samples has the goal to show how to use Cimbalino Toolkit - ShareLinkService.  
  40.         </TextBlock>  
  41.         <!--  ContentPanel - place additional content here  -->  
  42.         <Grid x:Name="ContentPanel"  
  43.               Grid.Row="1"  
  44.               Margin="12,0,12,0" />  
  45.         <i:Interaction.Behaviors>  
  46.             <cimbalino:ApplicationBarBehavior>  
  47.    
  48.                 <cimbalino:ApplicationBarIconButton Command="{Binding ShareSocialNetworkCommand,  
  49.                                                                       Mode=OneTime}"  
  50.                                                     IconUri="/Images/appbar.share.png"  
  51.                                                     Text="Share it" />  
  52.             </cimbalino:ApplicationBarBehavior>  
  53.         </i:Interaction.Behaviors>  
  54.     </Grid>  
  55.    
  56. </phone:PhoneApplicationPage>  

 

Up Next
    Ebook Download
    View all
    Learn
    View all