
- Windows 10 開發教程
- Windows 10 - 首頁
- Windows 10 - 簡介
- Windows 10 – UWP
- Windows 10 – 第一個應用
- Windows 10 - 應用商店
- Windows 10 - XAML 控制元件
- Windows 10 - 資料繫結
- Windows 10 - XAML 效能
- Windows 10 - 自適應設計
- Windows 10 - 自適應 UI
- Windows 10 - 自適應程式碼
- Windows 10 - 檔案管理
- Windows 10 - SQLite 資料庫
- Windows 10 – 通訊
- Windows 10 - 應用本地化
- Windows 10 - 應用生命週期
- Windows 10 - 後臺執行
- Windows 10 - 應用服務
- Windows 10 - Web 平臺
- Windows 10 - 連線體驗
- Windows 10 - 導航
- Windows 10 - 網路
- Windows 10 - 雲服務
- Windows 10 - 動態磁貼
- Windows 10 - 共享契約
- Windows 10 - 移植到 Windows
- Windows 10 有用資源
- Windows 10 - 快速指南
- Windows 10 - 有用資源
- Windows 10 - 討論
Windows 10 開發 - 應用通訊
應用間通訊是指您的應用程式可以與安裝在同一裝置上的另一個應用程式進行通訊。這並非通用 Windows 平臺 (UWP) 應用程式的新功能,在 Windows 8.1 中也可用。
在 Windows 10 中,引入了一些新的改進方法,以便輕鬆地在同一裝置上的應用程式之間進行通訊。兩個應用程式之間的通訊可以透過以下方式進行:
- 一個應用程式啟動另一個應用程式並傳遞一些資料。
- 應用程式簡單地交換資料,無需啟動任何內容。
應用間通訊的主要優點是您可以將應用程式分解成更小的塊,這些塊可以更容易地維護、更新和使用。
準備您的應用
如果您按照以下步驟操作,其他應用程式就可以啟動您的應用程式。
在應用程式包清單中新增協議宣告。
雙擊解決方案資源管理器中提供的Package.appxmanifest檔案,如下所示。
轉到宣告選項卡,並寫入協議名稱,如下所示。

下一步是新增啟用程式碼,以便應用程式在被其他應用程式啟動時能夠做出相應的響應。
要響應協議啟用,我們需要重寫啟用類的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"));
讓我們透過一個簡單的示例來理解這一點,在這個示例中,我們有兩個 UWP 應用程式,分別是ProtocolHandlerDemo和FirstProtocolHandler。
在這個示例中,ProtocolHandlerDemo應用程式包含一個按鈕,單擊該按鈕將開啟FirstProtocolHandler應用程式。
下面是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>
下面顯示了App.xaml.cs檔案的 C# 實現,其中重寫了OnActivated。在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應用程式時,您將看到以下視窗。

現在,當您單擊按鈕時,它將開啟FirstProtocolHandler應用程式,如下所示。
