Windows10-development-app-to-app-communication

提供:Dev Guides
移動先:案内検索

Windows10 Dev-アプリ通信

アプリ間通信とは、同じデバイスにインストールされている別のアプリケーションとアプリケーションが通信できることを意味します。 これは、ユニバーサルWindowsプラットフォーム(UWP)アプリケーションの新機能ではなく、Windows 8.1でも利用可能でした。

Windows 10では、同じデバイス上のアプリケーション間で簡単に通信できるように、いくつかの新しい改良された方法が導入されています。 2つのアプリ間の通信は、次の方法で行うことができます-

  • あるアプリケーションがいくつかのデータで別のアプリを起動します。
  • アプリは、何も起動せずにデータを交換するだけです。

アプリからアプリへの通信の主な利点は、アプリケーションを小さなチャンクに分割できることです。これを簡単に維持、更新、使用できます。

アプリを準備する

以下の手順に従うと、他のアプリケーションがアプリケーションを起動できます。

  • アプリケーションパッケージマニフェストにプロトコル宣言を追加します。
  • Package.appxmanifest ファイルをダブルクリックします。このファイルは、次に示すようにソリューションエクスプローラーで使用できます。
  • *宣言*タブに移動し、以下に示すようにプロトコルの名前を書きます。

Getting App Ready

  • 次のステップは、*アクティベーション*コードを追加することです。これにより、アプリは他のアプリケーションによって起動されたときに適切に応答できます。
  • プロトコルのアクティベーションに応答するには、アクティベーションクラスの OnActivated メソッドをオーバーライドする必要があります。 そのため、 App.xaml.cs ファイルに次のコードを追加します。
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 initialization 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.Globalization.ApplicationLanguages.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();

   }
}
  • アプリケーションを起動するには、 Launcher.LaunchUriAsync メソッドを使用するだけで済み、このメソッドで指定されたプロトコルでアプリケーションを起動します。
await Windows.System.Launcher.LaunchUriAsync(new Uri("win10demo:?SomeData=123"));
*ProtocolHandlerDemo* と *FirstProtocolHandler* を備えた2つのUWPアプリケーションがある簡単な例でこれを理解しましょう。

この例では、 ProtocolHandlerDemo アプリケーションにはボタンが1つ含まれており、ボタンをクリックすると、 FirstProtocolHandler アプリケーションが開きます。

1つのボタンを含むProtocolHandlerDemoアプリケーションのXAMLコードを以下に示します。

<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-compatibility/2006"
   mc:Ignorable = "d">

   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <Button x:Name = "LaunchButton" Content = " Launch First Protocol App"
         FontSize = "24" HorizontalAlignment = "Center"
         Click = "LaunchButton_Click"/>
   </Grid>

</Page>

以下に示すのは、ボタンクリックイベントが実装されるC#コードです。

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

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

namespace ProtocolHandlerDemo {

  ///<summary>
     ///An empty page that can be used on its own or navigated to within a Frame.
  ///</summary>

   public sealed partial class MainPage : Page {

      public MainPage(){
         this.InitializeComponent();
      }

      private async void LaunchButton_Click(object sender, RoutedEventArgs e) {
         await Windows.System.Launcher.LaunchUriAsync(new
            Uri("win10demo:?SomeData=123"));
      }

   }
}

ここで、 FirstProtocolHandler アプリケーションテーブルを見てみましょう。 以下に、いくつかのプロパティを使用してテキストブロックが作成されるXAMLコードを示します。

<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-compatibility/2006"
   mc:Ignorable = "d">

   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <TextBlock Text = "You have successfully launch First Protocol Application"
         TextWrapping = "Wrap" Style = "{StaticResource SubtitleTextBlockStyle}"
         Margin = "30,39,0,0" VerticalAlignment = "Top" HorizontalAlignment = "Left"
         Height = "100" Width = "325"/>
   </Grid>

</Page>
*OnActicated* がオーバーライドされた *App.xaml.cs* ファイルのC#実装を以下に示します。 *App.xaml.cs* ファイルのAppクラス内に次のコードを追加します。
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 initialization 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.Globalization.ApplicationLanguages.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();
   }
}

エミュレータで ProtocolHandlerDemo アプリケーションをコンパイルして実行すると、次のウィンドウが表示されます。

App Ready Executeの取得

これで、ボタンをクリックすると、以下に示すように FirstProtocolHandler アプリケーションが開きます。

ボタンでアプリの準備をする