📅  最后修改于: 2020-11-18 10:09:52             🧑  作者: Mango
应用程序到应用程序的通信意味着您的应用程序可以与安装在同一设备上的另一个应用程序通话或通信。这不是通用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"));
让我们用一个简单的示例来理解这一点,在该示例中,我们有两个带有ProtocolHandlerDemo和FirstProtocolHandler的UWP应用程序。
在此示例中, ProtocolHandlerDemo应用程序包含一个按钮,然后单击该按钮将打开FirstProtocolHandler应用程序。
下面给出了ProtocolHandlerDemo应用程序中的XAML代码,其中包含一个按钮。
下面给出的是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 {
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
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代码,其中创建了具有某些属性的文本块。
下面显示了其中重写了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应用程序时,将看到以下窗口。
现在,当您单击按钮时,它将打开FirstProtocolHandler应用程序,如下所示。