uwp - AppServiceConnection可以双向通信吗?

标签 uwp windows-10 desktop-bridge

我有一个托管应用程序服务的主机 UWP 应用程序,在其 App.xaml.cs 中我有以下代码: 私有(private)BackgroundTaskDeferral _appServiceDeferral; 私有(private) AppServiceConnection _appServiceConnection; protected 覆盖 void OnBackgroundActivated(BackgroundActivatedEventArgs args) { base.OnBackgroundActivated(参数); 如果(args.TaskInstance.TriggerDetails 是 AppServiceTriggerDetails) { _appServiceDeferral = args.TaskInstance.GetDeferral(); args.TaskInstance.Canceled += OnAppServicesCanceled; AppServiceTriggerDetails details = args.TaskInstance.TriggerDetails as AppServiceTriggerDetails; _appServiceConnection = details.AppServiceConnection; _appServiceConnection.RequestReceived += Connection_RequestReceived; _appServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed; }

我的主机应用程序是一个 UWP 应用程序,它会在某个时间启动一个 win32 应用程序,Win32 是一个客户端应用程序。我有以下 Win32 应用程序代码:

connection = new AppServiceConnection(); connection.AppServiceName = "MyTestCommunicationService"; connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName; connection.RequestReceived += ClientConnection_RequestReceived; connection.ServiceClosed += Connection_ServiceClosed;

        var status = await connection.OpenAsync().AsTask();

在 UWP 主机应用程序中,我可以向 win32 应用程序发送消息:

        if (_appServiceConnection!= null)
        {
            ValueSet valueSet = new ValueSet();
            valueSet.Add("Command", dataContent);
            AppServiceResponse response = await _appServiceConnection.SendMessageAsync(valueSet);
            _isSuccess = response.Status == AppServiceResponseStatus.Success;
        }

我看到很多例子,客户端向应用程序连接的主机应用程序发送请求,然后主机应用程序处理请求。我想知道我的代码是否是在主机应用程序和 win32 应用程序之间进行双向通信的正确方法。我测试了它正在运行的代码,但想知道有关应用服务连接的最佳实践。

最佳答案

是的,AppServiceConnection 是一个双向通信管道。双方都有一个 AppServiceConnection 实例,可用于向另一方发起请求,以及接收来自另一方的请求。

关于uwp - AppServiceConnection可以双向通信吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939592/

相关文章:

c# - Windows 10 uwp粘性后台服务?

javascript - 在 UWP 中使用 Composition 制作移动圆圈

winapi - 创建没有标题栏、可调整大小边框且没有虚假 6px 白色条纹的窗口

winapi - Windows 10 上的 DwmExtendFrameIntoClientArea 奇怪行为

c# - 启动应用程序在注销和重新登录之前不会启动

httpclient - 如何设置具有基本授权的 Windows.Web.Http.HttpClient PostAsync++

c# - .net-native enum.GetValues 麻烦

c# - 开发始终在桌面上的 Windows 10 小部件应用程序

merge - 桌面桥 - 升级设置不起作用

uwp - Windows 应用商店应用程序 - 更新 Hook 和触发器