Edge本机消息传递的App Service桌面桥不起作用



我正在Edge中的Webextension和桌面应用程序之间开发本机消息传递。但是通过桌面与桌面应用程序的通信无效。在下面的代码中,显示了消息框Success,但未显示消息框Test

AppServiceResponse desktopBridgeResponse = await this.desktopBridgeConnection.SendMessageAsync(testValueSet);陷入异常"对象引用未设置为对象的实例"。

// AppService.cs
public async void Run(IBackgroundTaskInstance taskInstance)
{
    if (appService.CallerPackageFamilyName == "Microsoft.MicrosoftEdge_8wekyb3d8bbwe") // App service connection from Edge
    {
        appServiceconnection = details.AppServiceConnection;
        appServiceconnection.RequestReceived += OnRequestReceived;
        try
        {
            // Make sure the DesktopApp.exe is in your AppX folder, if not rebuild the solution
            await Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
        }
        catch (Exception)
        {
            MessageDialog dialog = new MessageDialog("Rebuild the solution and make sure the DesktopApp.exe is in your AppX folder");
            await dialog.ShowAsync();
        }
    }
    else (appService.CallerPackageFamilyName == Windows.ApplicationModel.Package.Current.Id.FamilyName) // App service connection from desktopBridge App
    {
        desktopBridgeConnection = details.AppServiceConnection;
    }
}
private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
    try
    {
        AppServiceResponse desktopBridgeResponse = await this.desktopBridgeConnection.SendMessageAsync(testValueSet);
    }
    catch (Exception e)
    {
        var errorMessage = e.Message;
    }
}
// DesktopApp.cs
[STAThread]
static void Main()
{
    Thread appServiceThread = new Thread(new ThreadStart(InitializeAppServiceConnection));
    appServiceThread.Start();
    Application.Run();
}
static async void InitializeAppServiceConnection()
{
    connection = new AppServiceConnection();
    connection.AppServiceName = "example";
    connection.PackageFamilyName = Package.Current.Id.FamilyName;
    connection.RequestReceived += Connection_RequestReceived;
    AppServiceConnectionStatus status = await connection.OpenAsync();
    if (status != AppServiceConnectionStatus.Success)
    {
        // something went wrong ...
        MessageBox.Show(status.ToString());
    }
    else
    {
        MessageBox.Show("Success");
    }
}
private static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
    MessageBox.Show("Test");
    string key = args.Request.Message.First().Key;
    string value = args.Request.Message.First().Value.ToString();
}

根据安全输入,应起作用。

尽管已成功建立了与应用程序服务的连接?

为什么未输入Connection_RequestReceived()

任何帮助将不胜感激,谢谢!

update :通过调试,我找到了以下内容:当应用程序服务从Edge接收消息时,Run()方法将运行到" Edge Connection Branch"中。接下来,执行OnRequestReceived(),其中将消息发送到Desktopbridge await this.desktopBridgeConnection.SendMessageAsync(testValueSet),但这会失败。然后,再次执行Run()并进入" Desktopbridge Connection分支"。此时,desktopBridgeConnection已定义,但为时已晚。

我需要在输入OnRequestReceived()之前定义desktopBridgeConnection,以便可以在OnRequestReceived()中使用它。但是我不知道该如何实施。

您需要移动启动完整Trust进程的代码((,并等待通过AppServiceConnection连接的启动过程。然后将命令发送到启动过程并获取响应并将其发送回页面。

请参阅我的问题和我在这里设计的解决方案。

本质上,我使用了非持久连接。因此,您需要删除与Content.JS和Background.JS的连接/断开连接的所有代码。这使其成为一个无会的"同步"调用,一直向下和向后。

祝你好运!

相关内容

  • 没有找到相关文章

最新更新