Opening an app from other app using protocol

BitSmithy 2,016 Reputation points
2024-07-25T18:55:44.9433333+00:00

Hello,

I am trying to open app from other app where the opened app has a protocol defined in its manifest.

I use such code:

        public async void OpenOrShowInStore(string customCampaignId)
        {
            //if not instaled go to
            LauncherOptions options = new LauncherOptions();

            if (customCampaignId == "")
            {
                options.FallbackUri = appWinStoreProtocolUri;
            }
            else
            {
                if (Uri.TryCreate(appWinStoreProtocolUri.OriginalString + "&cid=" + customCampaignId, UriKind.RelativeOrAbsolute, out Uri newUri))
                {
                    var a = newUri.OriginalString;

                    options.FallbackUri = newUri;

                }
                else
                    options.FallbackUri = appWinStoreProtocolUri;
            }

            var success = await Windows.System.Launcher.LaunchUriAsync(appUri, options);
        }

The protocol is defined in opened app manifest in declaration section.

Main app is instaled as flight package, opened app is instaled as flight package too.

OnActivated(IActivatedEventArgs args) event isnt implemented in opened app because I do nothing when I open the app, I want simplu opent it.

Result of my code is such that opened app opens, but is stuck on splash screen.

How to fix this problem?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 16,856 Reputation points Microsoft Vendor
    2024-07-26T08:56:00.23+00:00

    Hi @BitSmithy ,

    Welcome to Microsoft Q&A!

    OnActivated(IActivatedEventArgs args) event isnt implemented in opened app because I do nothing when I open the app, I want simplu opent it.

    I think this is the reason for the unexpected behavior, you did nothing in OnActivated.

    Refer to the official sample Association launching sample and you should initialize the root Frame and activate the Window.

    You can try adding this code in the opened app(App.xaml.cs). I tested it and it works

    protected override void OnActivated(IActivatedEventArgs e)
    {
        if (e.Kind == ActivationKind.Protocol)
        {
            Frame rootFrame = CreateRootFrame();
            if (rootFrame.Content == null)
            {
                if (!rootFrame.Navigate(typeof(MainPage)))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
            rootFrame.Navigate(typeof(MainPage), e);
            // Ensure the current window is active
            Window.Current.Activate();
        }
    }
    private Frame CreateRootFrame()
    {
        Frame rootFrame = Window.Current.Content as Frame;
        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            // Set the default language
            rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
            rootFrame.NavigationFailed += OnNavigationFailed;
            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }
        return rootFrame;
    }
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful