0
votes

Can we update the UWP TextBlock's text from AppService?

Here I'm having the app service in different UWP Runtime Component project and that is added as reference to the UI project.

So I don't have direct connection with UI. But I need to update UI if the App is running as active.

Note: We may show the Toast and Live Tile instead. But want to know the possibility to directly update the UI.

Edited: [18/3/18]

I'm trying to merge existing WPF app within UWP using DesktopBridge. Here I need to have two way communication between both apps.

So created two AppServices separately in WPF and UWP to send and receive data in form of ValueSet and based on the requested data need to update the UI.

For WPF App having the UI and AppService in same project. For UWP App separated the AppServices as Runtime Component project (Out-Of-Proc). In this case both AppServices are working fine at runtime but cannot update the UWP UI (But able to update the WPF UI).

Also I tried to have in-proc services, in this case both AppServices are working fine but I cannot establish a connection from UWP to WPF. So I cannot send request from UWP (WPF to UWP is working).

So this is my actual problem. Want to know am I doing right? And is there any way to fix or any other better way to do it.

Followings are the .appmanifest code.

As In-Proc service

<Extensions>
    <uap:Extension Category="windows.appService">
        <uap:AppService Name="com.mycompany.scanner.uwpappservice" />
    </uap:Extension>
    <uap:Extension Category="windows.appService">
        <uap:AppService Name="com.mycompany.scanner.wpfappservice" />
    </uap:Extension>
    <desktop:Extension Category="windows.fullTrustProcess" Executable="Scanner.WPF.exe" />
</Extensions>

As Out-Of-Proc service

<Extensions>
    <uap:Extension Category="windows.appService" EntryPoint="UWPApp.AppServiceLibrary.UWPAppService">
        <uap:AppService Name="com.mycompany.scanner.uwpappservice" />
    </uap:Extension>
    <uap:Extension Category="windows.appService">
        <uap:AppService Name="com.mycompany.scanner.wpfappservice" />
    </uap:Extension>
    <desktop:Extension Category="windows.fullTrustProcess" Executable="Scanner.WPF.exe" />
</Extensions>
2

2 Answers

3
votes

You could use an In-process App Service as described in the documentation.

App Services can now run in the same process as the foreground application which makes communication between apps much easier and removes the need to separate the service code into a separate project.

This means you don't even have to move the code to a separate Windows Runtime Component and you may communicate with the UI in case it is active via Window.Current. However, you need to remember to make sure that your code manipulating the UI runs on the UI thread using Dispatcher.

2
votes

If you have configured your AppService to run in-proc with the foreground app then you can update the UI directly. If the AppService is running out-of-proc, then you would need to send a request to the foreground app to update it's UI on your behalf, or go with toast/tile notifications.

Here is some info on how to make your AppService run in-proc with the application process: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/convert-app-service-in-process

Regarding the edited question, the typical pattern to use is this: When the WPF app launches, open the app service connection to the UWP. Now on both sides keep a reference to the respective instance of the AppServiceConnection object. This way you can do two-way communication and either side (UWP and WPF) can establish a request to the other side.