English 中文(简体)
Windows 10 – Communication
  • 时间:2024-11-03

Windows10 Dev - App Communication


Previous Page Next Page  

App to app communication means that your apppcation can speak to or communicate with another apppcation that is installed on the same device. This is not a new feature in Universal Windows Platform (UWP) apppcation and was also available in Windows 8.1.

In Windows 10, some new and improved ways are introduced to easily communicate between apppcations on the same device. Communication between two apps can be in the following ways −

    One apppcation launching another app with some data.

    Apps are simply exchanging data without launching anything.

The main advantage of app to app communication is that you can break apppcations into smaller chunks, which can be maintained, updated and consumed easily.

Getting Your App Ready

If you Follow the steps given below, other apppcations can launch your apppcation.

    Add a protocol declaration in apppcation package manifest.

    Double cpck on the Package.appxmanifest file, which is available in the Solution Explorer as shown below.

    Go to the Declaration tab and write the Name of the protocol as shown below.

Getting App Ready

    The next step is to add the activation code, so the app can respond appropriately when launched by the other apppcation.

    To respond to protocol activations, we need to override the OnActivated method of the activation class. So, add the following code in App.xaml.cs file.

protected override void OnActivated(IActivatedEventArgs args) {
 
   ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs;
	
   if (args != null){ 

      Frame rootFrame = Window.Current.Content as Frame;
	  
      // Do not repeat app initiapzation when the Window already has content, 
      // just ensure that the window is active
	  
      if (rootFrame == null){ 
		 
         // Create a Frame to act as the navigation context and navigate to the first page
         rootFrame = new Frame(); 
		 
         // Set the default language 
         rootFrame.Language = Windows.Globapzation.ApppcationLanguages.Languages[0];  
         rootFrame.NavigationFailed += OnNavigationFailed;
		 
         // Place the frame in the current Window 
         Window.Current.Content = rootFrame; 
      } 
		
      if (rootFrame.Content == null){
	  
         // When the navigation stack isn t restored, navigate to the  
         // first page, configuring the new page by passing required  
         // information as a navigation parameter 
		 
         rootFrame.Navigate(typeof(MainPage), null); 
      } 
		
      // Ensure the current window is active 
      Window.Current.Activate(); 
		
   } 
} 

    To launch the apppcation, you can simply use the Launcher.LaunchUriAsync method, which will launch the apppcation with protocol specified in this method.

await Windows.System.Launcher.LaunchUriAsync(new Uri("win10demo:?SomeData=123"));

Let us understand this with a simple example in which we have two UWP apppcations with ProtocolHandlerDemo and FirstProtocolHandler.

In this example, the ProtocolHandlerDemo apppcation contains one button and by cpcking on the button, it will open the FirstProtocolHandler apppcation.

XAML code in the ProtocolHandlerDemo apppcation, which contains one button is given below.

<Page 
   x:Class = "ProtocolHandlerDemo.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "using:ProtocolHandlerDemo" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   mc:Ignorable = "d">  
   
   <Grid Background = "{ThemeResource ApppcationPageBackgroundThemeBrush}"> 
      <Button x:Name = "LaunchButton" Content = " Launch First Protocol App"
         FontSize = "24" HorizontalApgnment = "Center" 
         Cpck = "LaunchButton_Cpck"/> 
   </Grid> 
	
</Page>

Given below is the C# code, in which the button cpck event is implemented.

using System; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls;  

// The Blank Page item template is documented at 
   http://go.microsoft.com/fwpnk/?LinkId=402352&clcid=0x409  

namespace ProtocolHandlerDemo {

   /// <summary> 
      /// An empty page that can be used on its own or navigated to within a Frame. 
   /// </summary> 
   
   pubpc sealed partial class MainPage : Page {
   
      pubpc MainPage(){ 
         this.InitiapzeComponent(); 
      }
		
      private async void LaunchButton_Cpck(object sender, RoutedEventArgs e) {
         await Windows.System.Launcher.LaunchUriAsync(new 
            Uri("win10demo:?SomeData=123")); 
      }
		
   }
}

Now let us have a look into the FirstProtocolHandler apppcation table. Given below is the XAML code in which a textblock is created with some properties.

<Page 
   x:Class = "FirstProtocolHandler.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "using:FirstProtocolHandler" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   mc:Ignorable = "d">  
   
   <Grid Background = "{ThemeResource ApppcationPageBackgroundThemeBrush}"> 
      <TextBlock Text = "You have successfully launch First Protocol Apppcation" 
         TextWrapping = "Wrap" Style = "{StaticResource SubtitleTextBlockStyle}"  
         Margin = "30,39,0,0" VerticalApgnment = "Top" HorizontalApgnment = "Left" 
         Height = "100" Width = "325"/> 
   </Grid> 
	
</Page>

The C# implementation of the App.xaml.cs file in which OnActicated is overriden is shown below. Add the following code inside App class in the App.xaml.cs file.

protected override void OnActivated(IActivatedEventArgs args) { 
   ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs;
	
   if (args != null) {
      Frame rootFrame = Window.Current.Content as Frame;  
		
      // Do not repeat app initiapzation when the Window already has content, 
      // just ensure that the window is active 
		
      if (rootFrame == null) {

         // Create a Frame to act as the navigation context and navigate to 
            the first page 
         rootFrame = new Frame(); 
		 
         // Set the default language 
         rootFrame.Language = Windows.Globapzation.ApppcationLanguages.Languages[0];  
         rootFrame.NavigationFailed += OnNavigationFailed; 
		 
         // Place the frame in the current Window 
         Window.Current.Content = rootFrame; 
      }  
		
      if (rootFrame.Content == null) {
		
         // When the navigation stack isn t restored navigate to the 
         // first page, configuring the new page by passing required 
         // information as a navigation parameter 
		 
         rootFrame.Navigate(typeof(MainPage), null); 
      }
		
      // Ensure the current window is active 
      Window.Current.Activate(); 
   } 
} 

When you compile and execute the ProtocolHandlerDemo apppcation on an emulator, you will see the following window.

Getting App Ready Execute

Now, when you cpck on the button, it will open the FirstProtocolHandler apppcation as shown below.

Getting App Ready With Button Advertisements