Home > C#, WinRT > Make an app respond to URL requests using Protocol declaration

Make an app respond to URL requests using Protocol declaration


Using the appropriate declaration, Windows Store apps can been invoked in response of specific URL requests. For example, our app can be automatically activated when the user selects a link that starts with myapp://.

This is possible through a Protocol declaration. Let’s see how it works. Start creating a new Windows Store app. For our purpose, a Blank App is enough. Now, we need to open the Package.appxmanifest file, go to Declarations tab and add Protocol from the Available Declarations list. Then, we specify the URI schema we want to handle in the Name property:


After that, we need to override the OnActivated method in App.xaml.cs class and check the activation kind:

protected override void OnActivated(IActivatedEventArgs args)
    if (args.Kind == ActivationKind.Protocol)
        // Retrieves the activation Uri.
        var protocolArgs = (ProtocolActivatedEventArgs)args;
        var uri = protocolArgs.Uri;

        var frame = Window.Current.Content as Frame;

        if (frame == null)
            frame = new Frame();

        // Navigates to MainPage, passing the Uri to it.
        frame.Navigate(typeof(MainPage), uri);
        Window.Current.Content = frame;

        // Ensure the current window is active


The important line is 7, in which we retrieve the complete URL that has been used to invoke our application, for example myapp://message/custom_message. We pass this value to the MainPage. To verify that everything works correctly,we use a TextBlock in which we’ll show how the application has been activated:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock x:Name="ActivationArgs" HorizontalAlignment="Center" 
            VerticalAlignment="Center" FontSize="20"></TextBlock>

Finally, in the OnNavigatedTo method of MainPage.xaml.cs, we check whether an Uri has been passed as parameter, i.e. the app has been activated through an URL request:

protected override void OnNavigatedTo(NavigationEventArgs e)
    if (e.Parameter is Uri)
        ActivationArgs.Text = "The app has been invoked using the following URL request: " 
                                    + e.Parameter.ToString();
        ActivationArgs.Text = "Normal application activation";

To test the app, we can use the Run window and write an URL that is handled by our app:


When we press the OK button, our app will be launched and the message The app has been invoked using the following URL request: myapp://message/custom_message will be displayed.

This link schema can be used in the LaunchUriAsync method or even as an hyperlink in a standard HTML page. This means that, with Protocol declaration, we can activate our app from another one or from a Web page.

Categories: C#, WinRT
%d bloggers like this: