Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Forms: How to launch a UWP app from another UWP app?

SreeeeSreeee INMember ✭✭✭✭✭

I am trying to launch my UWP app from another UWP app. This feature is working fine for android and ios.

I am using this blog for the UWP section.

UWP Render Code

public class OpenAppImplementation : IAppHandler
{
    public async Task<bool> LaunchApp(string stringUri)
    {
        Uri uri = new Uri(stringUri);
        return await Windows.System.Launcher.LaunchUriAsync(uri);
    }
}

In the Main project

var appname = "UWP package name://";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
if (!result)
{
    Device.OpenUri(new Uri(windows store link));
}

I am passing the package name of the second app as appname here. If the app is installed in the device, I need to open that; else open the windows store app page for downloading the app.

When I execute this I am getting a screen like below:

enter image description here

Help me to solve this problem. Thanks in advance.

Tagged:

Best Answers

  • SreeeeSreeee INMember ✭✭✭✭✭
    Accepted Answer

    Add protocol for the launched app. For more refer this document.

    Steps:

    Specify the extension point in the package manifest like the following.

    <Applications>
        <Application Id= ... >
            <Extensions>
                <uap:Extension Category="windows.protocol">
                  <uap:Protocol Name="alsdk">
                    <uap:Logo>images\icon.png</uap:Logo>
                    <uap:DisplayName>SDK Sample URI Scheme</uap:DisplayName>
                  </uap:Protocol>
                </uap:Extension>
          </Extensions>
          ...
        </Application>
    

    And you could also use visual studio manifest GUI to add the protocol.

    If you have not specified the parameter you could remove // , just keep URI scheme like alsdk:.

    In target app we need process OnActivated method and invoke Window.Current.Activate() finally.

    protected override void OnActivated(IActivatedEventArgs args)
    {
        if (args.Kind == ActivationKind.Protocol)
        {
            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
            // Navigate to a view 
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Xamarin.Forms.Forms.Init(args);
                Window.Current.Content = rootFrame;
            }
            // assuming you wanna go to MainPage when activated via protocol
            rootFrame.Navigate(typeof(MainPage), eventArgs);
        }
        Window.Current.Activate();
    }
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    First of all , you need to specify the extension point in the package manifest to let your app to become the default handler for a Uniform Resource Identifier (URI) scheme name .

    Under the extension point you need to set a Protocol which indicates that the app handles the alsdk URI scheme name.

    The following sample shows how to register the custom url name alsdk .

    <Applications>
        <Application Id= ... >
            <Extensions>
                <uap:Extension Category="windows.protocol">
                  <uap:Protocol Name="alsdk">
                    <uap:Logo>images\icon.png</uap:Logo>
                    <uap:DisplayName>SDK Sample URI Scheme</uap:DisplayName>
                  </uap:Protocol>
                </uap:Extension>
          </Extensions>
          ...
        </Application>
      <Applications>
    

    Details refer to https://docs.microsoft.com/en-us/windows/uwp/launch-resume/handle-uri-activation#step-1-specify-the-extension-point-in-the-package-manifest .

  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX I have added the protocol declaration on the second app as below:

    enter image description here

    Then on the first app call like below:

    var appname = "alsdk:";
    var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
    if (!result)
    {
        Device.OpenUri(new Uri("windows store link"));
    }
    

    The second app is opening and the splash screen is showing after the above changes. But not going to the home page, the app is staying in the splash screen. Also, I need to show the windows store app page if the app is not installed on the device. What I am missing here, please help.

  • SreeeeSreeee INMember ✭✭✭✭✭
    Accepted Answer

    Add protocol for the launched app. For more refer this document.

    Steps:

    Specify the extension point in the package manifest like the following.

    <Applications>
        <Application Id= ... >
            <Extensions>
                <uap:Extension Category="windows.protocol">
                  <uap:Protocol Name="alsdk">
                    <uap:Logo>images\icon.png</uap:Logo>
                    <uap:DisplayName>SDK Sample URI Scheme</uap:DisplayName>
                  </uap:Protocol>
                </uap:Extension>
          </Extensions>
          ...
        </Application>
    

    And you could also use visual studio manifest GUI to add the protocol.

    If you have not specified the parameter you could remove // , just keep URI scheme like alsdk:.

    In target app we need process OnActivated method and invoke Window.Current.Activate() finally.

    protected override void OnActivated(IActivatedEventArgs args)
    {
        if (args.Kind == ActivationKind.Protocol)
        {
            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
            // Navigate to a view 
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Xamarin.Forms.Forms.Init(args);
                Window.Current.Content = rootFrame;
            }
            // assuming you wanna go to MainPage when activated via protocol
            rootFrame.Navigate(typeof(MainPage), eventArgs);
        }
        Window.Current.Activate();
    }
    
  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX I am able to open the app after adding the Protocol and OnActivated method. But if the app is not installed on the device I need to redirect the user to the windows store app page. That part is not working.

    var appname = "alsdk:";
    var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
    if (!result)
    {
        Device.OpenUri(new Uri("windows store link"));
    }
    

    The value of the result is always true.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited October 8

    @ColeX Please help me to redirect the user to the windows store page, if the app is not installed in the device. The value of the result is always true even the app is not installed on the device. Please watch this video.

Sign In or Register to comment.