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,39 @@ 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+ await args . Request . SendResponseAsync ( BadResult ) ;
48+ messageDeferral . Complete ( ) ;
49+ return ;
50+ }
4351
44- try
52+ switch ( cmd )
4553 {
46- // Return the data to the caller.
47- if ( cmd == "Start" )
48- {
49- var pageName = message [ "Page" ] as string ;
54+ case "OpenPage" :
55+ if ( ! TryGetValueAndLog ( message , "Page" , out var pageName ) )
56+ {
57+ await args . Request . SendResponseAsync ( BadResult ) ;
58+ break ;
59+ }
5060
5161 Log . Comment ( "Received request for Page: {0}" , pageName ) ;
5262
53- ValueSet returnMessage = new ValueSet ( ) ;
54-
5563 // 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- }
64+ var pageResponse = await WeakReferenceMessenger . Default . Send ( new RequestPageMessage ( pageName ) ) ;
6465
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 ( ) ;
66+ await args . Request . SendResponseAsync ( pageResponse ? OkResult : BadResult ) ;
67+
68+ break ;
69+ default :
70+ break ;
7871 }
72+
73+ // Complete the deferral so that the platform knows that we're done responding to the app service call.
74+ // Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
75+ messageDeferral . Complete ( ) ;
7976 }
8077
8178 private void OnAppServicesCanceled ( IBackgroundTaskInstance sender , BackgroundTaskCancellationReason reason )
@@ -88,16 +85,38 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
8885 _appServiceDeferral . Complete ( ) ;
8986 }
9087
91- public async void SendLogMessage ( string level , string msg )
88+ public async Task SendLogMessage ( string level , string msg )
9289 {
93- var message = new ValueSet ( ) ;
94- message . Add ( "Command" , "Log" ) ;
95- message . Add ( "Level" , level ) ;
96- message . Add ( "Message" , msg ) ;
90+ var message = new ValueSet
91+ {
92+ { "Command" , "Log" } ,
93+ { "Level" , level } ,
94+ { "Message" , msg }
95+ } ;
9796
9897 await _appServiceConnection . SendMessageAsync ( message ) ;
9998
10099 // TODO: do we care if we have a problem here?
101100 }
101+
102+ private static bool TryGetValueAndLog ( ValueSet message , string key , out string value )
103+ {
104+ value = null ;
105+ if ( ! message . TryGetValue ( key , out var o ) )
106+ {
107+ Log . Error ( $ "Could not find the key \" { key } \" in the message.") ;
108+ return false ;
109+ }
110+
111+ if ( o is not string s )
112+ {
113+ Log . Error ( $ "{ key } 's value is not a string") ;
114+ return false ;
115+ }
116+
117+ value = s ;
118+
119+ return true ;
120+ }
102121 }
103122}
0 commit comments