【问题标题】:Xamarin Forms: How to launch a UWP app from another UWP app?Xamarin Forms:如何从另一个 UWP 应用启动 UWP 应用?
【发布时间】:2021-01-20 06:31:09
【问题描述】:

我正在尝试从另一个 UWP 应用启动我的 UWP 应用。此功能适用于 androidios

我在 UWP 部分使用 this 博客。

UWP 渲染代码

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

在主项目中

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

我在这里将第二个应用程序的包名称传递为appname。如果应用程序安装在设备中,我需要打开它;否则打开windows store应用页面下载应用。

当我执行此操作时,我得到如下屏幕:

更新

我在第二个应用上添加了如下协议声明:

然后在第一个应用调用如下:

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

第二个应用程序正在打开,并在上述更改后显示启动画面。但没有进入主页,应用程序停留在启动画面中。此外,如果设备上未安装应用程序,我需要显示 Windows 商店应用程序页面。我在这里缺少什么?

【问题讨论】:

    标签: xamarin.forms uwp


    【解决方案1】:

    问题是你传递了错误的 uri 方案,或者你没有为启动的应用注册协议。更多信息请参考document

    步骤:

    在包清单中指定扩展点,如下所示。

    <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>
    

    您还可以使用 Visual Studio 清单 GUI 来添加协议。

    如果您没有指定可以删除 // 的参数,只需保留类似 alsdk: 的 uri 方案。

    更新

    第二个应用程序正在打开,并在上述更改后显示启动画面。但没有进入主页,应用程序停留在启动画面中。此外,如果设备上未安装应用程序,我需要显示 Windows 商店应用程序页面。我在这里缺少什么?

    在目标应用程序中,我们需要处理OnActivated 方法并最终调用Window.Current.Activate()

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

    更新

    如果您启动 xamarin uwp 应用程序,您需要在 OnActivated 方法中初始化 Xamarin Form 组件。

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

    【讨论】:

    • 请注意上面的代码需要添加到目标uwp应用程序中,添加上面之后我们需要重新构建目标应用程序。
    • 你可以使用windows系统runwindow来测试上面的uri方案。按Win + R 并输入 alsdk: 然后按回车键。它将午餐目标应用程序。
    • 我正在尝试从A 应用打开B 应用。我在B 应用程序上添加了protocolOnActivated 方法。当我尝试从A 打开B 应用程序时,只显示启动屏幕。一段时间后,B 应用程序将退出。当按下Win+R 并输入alsdk: 时也会发生同样的事情
    • 能否设置调试点 OnActivated 方法来检测它是否被调用?
    • 是的,我知道,因为它默认启动了applicationpicker,所以返回true,对于这种情况我们建议你使用Launch an app for results
    猜你喜欢
    • 2018-12-08
    • 2017-10-12
    • 2021-08-21
    • 2018-10-21
    • 2016-02-01
    • 2016-12-02
    • 2018-06-17
    • 2017-07-11
    • 2020-07-22
    相关资源
    最近更新 更多