33// See the LICENSE file in the project root for more information.
44
55using System ;
6- using System . Diagnostics ;
6+ using System . Threading . Tasks ;
77using Microsoft . Toolkit . Mvvm . Messaging ;
88using UITests . App . Pages ;
99using Windows . ApplicationModel . Activation ;
@@ -20,6 +20,9 @@ namespace UITests.App
2020 /// </summary>
2121 public sealed partial class App
2222 {
23+ private static readonly ValueSet BadResult = new ( ) { { "Status" , "BAD" } } ;
24+ private static readonly ValueSet OkResult = new ( ) { { "Status" , "OK" } } ;
25+
2326 private AppServiceConnection _appServiceConnection ;
2427 private BackgroundTaskDeferral _appServiceDeferral ;
2528
@@ -37,45 +40,42 @@ protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
3740
3841 private async void OnAppServiceRequestReceived ( AppServiceConnection sender , AppServiceRequestReceivedEventArgs args )
3942 {
40- AppServiceDeferral messageDeferral = args . GetDeferral ( ) ;
41- ValueSet message = args . Request . Message ;
42- string cmd = message [ "Command" ] as string ;
43+ var messageDeferral = args . GetDeferral ( ) ;
44+ var message = args . Request . Message ;
45+ if ( ! TryGetValueAndLog ( message , "Command" , out var cmd ) )
46+ {
47+ messageDeferral . Complete ( ) ;
48+ return ;
49+ }
4350
44- try
51+ switch ( cmd )
4552 {
46- // Return the data to the caller.
47- if ( cmd == "Start" )
48- {
49- var pageName = message [ "Page" ] as string ;
53+ case "OpenPage" :
54+ if ( ! TryGetValueAndLog ( message , "Page" , out var pageName ) )
55+ {
56+ await args . Request . SendResponseAsync ( BadResult ) ;
57+ break ;
58+ }
5059
5160 Log . Comment ( "Received request for Page: {0}" , pageName ) ;
5261
53- ValueSet returnMessage = new ValueSet ( ) ;
54-
5562 // We await the OpenPage method to ensure the navigation has finished.
56- if ( await WeakReferenceMessenger . Default . Send ( new RequestPageMessage ( pageName ) ) )
57- {
58- returnMessage . Add ( "Status" , "OK" ) ;
59- }
60- else
61- {
62- returnMessage . Add ( "Status" , "BAD" ) ;
63- }
63+ var pageResponse = await WeakReferenceMessenger . Default . Send ( new RequestPageMessage ( pageName ) ) ;
6464
65- await args . Request . SendResponseAsync ( returnMessage ) ;
66- }
67- }
68- catch ( Exception e )
69- {
70- // Your exception handling code here.
71- Log . Error ( "Exception processing request: {0}" , e . Message ) ;
72- }
73- finally
74- {
75- // Complete the deferral so that the platform knows that we're done responding to the app service call.
76- // Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
77- messageDeferral . Complete ( ) ;
65+ await args . Request . SendResponseAsync ( pageResponse ? OkResult : BadResult ) ;
66+
67+ break ;
68+ case "Close" :
69+ Current . Exit ( ) ;
70+ await args . Request . SendResponseAsync ( OkResult ) ;
71+ break ;
72+ default :
73+ break ;
7874 }
75+
76+ // Complete the deferral so that the platform knows that we're done responding to the app service call.
77+ // Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
78+ messageDeferral . Complete ( ) ;
7979 }
8080
8181 private void OnAppServicesCanceled ( IBackgroundTaskInstance sender , BackgroundTaskCancellationReason reason )
@@ -88,16 +88,38 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
8888 _appServiceDeferral . Complete ( ) ;
8989 }
9090
91- public async void SendLogMessage ( string level , string msg )
91+ public async Task SendLogMessage ( string level , string msg )
9292 {
93- var message = new ValueSet ( ) ;
94- message . Add ( "Command" , "Log" ) ;
95- message . Add ( "Level" , level ) ;
96- message . Add ( "Message" , msg ) ;
93+ var message = new ValueSet
94+ {
95+ { "Command" , "Log" } ,
96+ { "Level" , level } ,
97+ { "Message" , msg }
98+ } ;
9799
98100 await _appServiceConnection . SendMessageAsync ( message ) ;
99101
100102 // TODO: do we care if we have a problem here?
101103 }
104+
105+ private static bool TryGetValueAndLog ( ValueSet message , string key , out string value )
106+ {
107+ value = null ;
108+ if ( ! message . TryGetValue ( key , out var o ) )
109+ {
110+ Log . Error ( $ "Could not find the key \" { key } \" in the message.") ;
111+ return false ;
112+ }
113+
114+ if ( o is not string s )
115+ {
116+ Log . Error ( $ "{ key } 's value is not a string") ;
117+ return false ;
118+ }
119+
120+ value = s ;
121+
122+ return true ;
123+ }
102124 }
103125}
0 commit comments