Any app may request that the OS launch a protocol URI. This is achieved using the Windows.System.Launcher
class.
The following excerpt from the MainPageViewModel constructs a URI by appending the protocol name with a custom message. The Launcher’s LaunchFileAsync
method is then called like so:
void LaunchProtocol()
{
Launcher.LaunchUriAsync(new Uri("unleashed:" + protocolMessage));
}
The viewmodel contains a string property named ProtocolMessage
, which allows the user to compose a message to be sent to a registered app for the protocol.
The view contains a TextBox
that is bound to the ProtocolMessage
property as shown:
<TextBox Text="{Binding ProtocolMessage, Mode=TwoWay}"
Width="250"/>
The viewmodel contains an ICommand
named launchProtocolCommand
that calls the LaunchProtocol
method:
launchProtocolCommand
= new DelegateCommand(obj => LaunchProtocol());
A Button
in the view is bound to the LaunchProtocolCommand
property:
<Button Content="Launch Protocol" Command="{Binding LaunchProtocolCommand}" />
Tapping the button causes the protocol URI to be constructed, and the app is deactivated. The OS dispatches the URI to the app that is registered to handle the unleashed protocol. The OS launches the WPUnleashed.FileAndProtocolAssociations, and the app navigates to the ProtocolMessageView.xaml page, which is displayed in Figure 30.2.
Protocol associations allow your app to communicate with other apps on the device and allow the user to select the app that best suits his or her needs for handling a particular protocol message.
18.219.64.172