Some new and improved ways have been introduced in Windows 10 to ease communication between the apps.
Previously, if we wanted to launch another app and make it do something, there was no programmatic way of notifying us, that the action was completed. With the new “launch for results” feature in Windows 10, we can get the callback from the second app, after some operation completes.
Windows 10 provides a great API, which can open another app and perform the operations in the second app and finally send results back to the first app. The new LaunchUriForResultsAsync method relies on the TargetApplicationPackageFamilyName API to make two-way communication between the apps possible.
Step 1: Open Visual Studio 2015. Create a Blank Universal Windows project, name it ‘App1’.
The first step is making the app available to be launched by other apps. To do this, we need to add a protocol declaration to the app’s package manifest (Package.appxmanifest).
In the Package.appmanifest of App1, we have to define the protocols like:
- <Extensions>
- <uap:Extension Category="windows.protocol">
- <uap:Protocol Name="app1" ReturnResults="optional">
- <uap:Logo>Assets\StoreLogo.scale-100.png</uap:Logo>
- <uap:DisplayName>App1</uap:DisplayName>
- </uap:Protocol>
- </uap:Extension>
- </Extensions>
In the Package.appmanifest of App2, define the protocols, similar to the one, shown above.
- <Extensions>
- <uap:Extension Category="windows.protocol">
- <uap:Protocol Name="app2" ReturnResults="optional">
- <uap:Logo>Assets\StoreLogo.scale-100.png</uap:Logo>
- <uap:DisplayName>App2</uap:DisplayName>
- </uap:Protocol>
- </uap:Extension>
- </Extensions>
Complete XAML code snippet
- <Page
- x:Class="App1.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:App1"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Button Click="Button_Click">Open another app</Button>
- </Grid>
- </Page>
- private async void Button_Click(object sender, RoutedEventArgs e)
- {
- var protocol = "app2://"; //protocol name of second app that is defined in package.appmanifest of second app.
- var packageFamilyName = "7df22c17-5ec3-4de0-b903-c295473af4a0_syk1cnben41w6"; //This is derived from second app.
- var status = await Launcher.QueryUriSupportAsync(new Uri(protocol), LaunchQuerySupportType.UriForResults, packageFamilyName);
- if (status == LaunchQuerySupportStatus.Available)
- {
- var options = new LauncherOptions
- {
- TargetApplicationPackageFamilyName = packageFamilyName
- };
- var values = new ValueSet();
- values.Add("UserName", "kishor");
- values.Add("Message", "This is message from App 1");
- var result = await Launcher.LaunchUriForResultsAsync(new Uri(protocol), options, values);
- if ((result.Status == LaunchUriStatus.Success) && (result.Result != null))
- {
- var isUser = result.Result["IsUser"] as string;
- var msg = result.Result["Message"] as string;
- if (isUser == "true")
- {
- var dialog = new MessageDialog(msg, "Success");
- await dialog.ShowAsync();
- }
- }
- }
- }
To get the package family name, we need this following code, which gives the package family name of the app.
- var packageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
Thus, put this code in App.xaml.cs of App2.
- protected override void OnActivated(IActivatedEventArgs args)
- {
- Frame rootFrame = Window.Current.Content as Frame;
- if (rootFrame == null)
- {
- rootFrame = new Frame();
- rootFrame.CacheSize = 1;
- Window.Current.Content = rootFrame;
- }
- if (args.Kind == ActivationKind.ProtocolForResults)
- {
- var pfrArgs = (ProtocolForResultsActivatedEventArgs)args;
- if (pfrArgs.CallerPackageFamilyName.Equals("6c2cea9e-22ae-4796-8cfa-5c29bfd3f557_syk1cnben41w6")) //Package Family name of first app
- {
- rootFrame.Navigate(typeof(MainPage), pfrArgs);
- }
- }
- else
- {
- rootFrame.Navigate(typeof(MainPage));
- }
- Window.Current.Activate();
- }
- <Page
- x:Class="App2.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:App2"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <StackPanel Orientation="Vertical">
- <TextBlock FontSize="50" >This is app 2</TextBlock>
- <TextBlock FontSize="30" Name="Name" ></TextBlock>
- <TextBlock FontSize="30" Name="Message" ></TextBlock>
- <Button x:Name="ok_Btn" Content="Ok" HorizontalAlignment="Left" FontSize="40" Click="ok_Btn_Click" VerticalAlignment="Top" Width="320"/>
- </StackPanel>
- </Grid>
- </Page>
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using Windows.ApplicationModel.Activation;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
- namespace App2
- {
- public sealed partial class MainPage : Page
- {
- private ProtocolForResultsActivatedEventArgs pfrArgs;
- private string userName;
- private string msg;
- public MainPage()
- {
- this.InitializeComponent();
- Loaded += MainPage_Loaded;
- }
- private void MainPage_Loaded(object sender, RoutedEventArgs e)
- {
- var packageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName; //To get the package family name of this app which is needed in first app.
- Debug.WriteLine(packageFamilyName);
- }
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- pfrArgs = e.Parameter as ProtocolForResultsActivatedEventArgs;
- if (pfrArgs != null)
- {
- userName = pfrArgs.Data["UserName"] as string;
- msg = pfrArgs.Data["Message"] as string;
- Name.Text = userName;
- Message.Text = msg;
- }
- }
- private void ok_Btn_Click(object sender, RoutedEventArgs e)
- {
- SuccessCallBack();
- }
- private void SuccessCallBack()
- {
- if (pfrArgs != null)
- {
- var values = new ValueSet();
- values.Add("Message", "This is message from app 2");
- values.Add("IsUser", "true");
- pfrArgs.ProtocolForResultsOperation.ReportCompleted(values);
- }
- }
- }
- }
Find the complete project in GitHub.

Delpin Susai RajPosted Aug 28, 2016, 3:49 AM
Nice one
Vignesh ManiPosted Aug 25, 2016, 6:29 AM
Nice