0
votes

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

Update

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?

1

1 Answers

3
votes

The problem is you have pass the wrong uri scheme or you have not registered protocol for the launched app. For more please 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 specific the parameter you could remove // , just keep uri scheme like alsdk:.

Update

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?

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();
            Window.Current.Content = rootFrame;
        }
        // assuming you wanna go to MainPage when activated via protocol
        rootFrame.Navigate(typeof(MainPage), eventArgs);

    }
    Window.Current.Activate();
}

Update

if you launch xamarin uwp app, you need initialize Xamarin Form component in the OnActivated method.

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();
}