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

1

0

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?

Bacchanal answered 5/10, 2020 at 11:34 Comment(0)
H
3

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();
}
Housework answered 6/10, 2020 at 2:28 Comment(14)
Please note above code need to be add into target uwp app, after add above we need re-build the target app.Housework
You could use windows system run window to test above uri scheme. press Win + R and type alsdk: then press enter key. it will lunch the target app.Housework
I am trying to open the B app from the A app. I have added the protocol and OnActivated method on B app. When I try to open the B app from A, only the launching screen is showing. After some time the B app is exiting. The same thing is happening when press Win+R and type alsdk:Bacchanal
Could set debug point OnActivated method to detect if it was invoked?Housework
ok please try to disable the main page 's parameter and test again.Housework
May I share a demo for you ?Housework
Let us continue this discussion in chat.Housework
Please check link here and the launch scheme is testapp:Housework
Please check latest update part, and add Xamarin.Forms.Forms.Init(args); into OnActivated method.Housework
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.Bacchanal
Please watch this video drive.google.com/file/d/1rNBp2Uad-QsLpU_IysZ_VfTyNoQ1YKo9/…Bacchanal
Yep, I know that, because it launched applicationpicker by default, so the return is true, For this scenario we suggest you use Launch an app for results.Housework
Currently, I am using the below code in the renderer. Can you update the answer with the LaunchUriForResultsAsync: public async Task<bool> LaunchApp(string stringUri) { Uri uri = new Uri(stringUri); return await Windows.System.Launcher.LaunchUriAsync(uri); }Bacchanal
Please check that document, it contains sample code, you could LaunchUriForResultsAsync to launch the target app, if the app has not installed, it will show a dialog automatically (search from store).Housework

© 2022 - 2024 — McMap. All rights reserved.