3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
- using System . Diagnostics ;
6
+ using System . Threading . Tasks ;
7
7
using Microsoft . Toolkit . Mvvm . Messaging ;
8
8
using UITests . App . Pages ;
9
9
using Windows . ApplicationModel . Activation ;
@@ -20,6 +20,9 @@ namespace UITests.App
20
20
/// </summary>
21
21
public sealed partial class App
22
22
{
23
+ private static readonly ValueSet BadResult = new ( ) { { "Status" , "BAD" } } ;
24
+ private static readonly ValueSet OkResult = new ( ) { { "Status" , "OK" } } ;
25
+
23
26
private AppServiceConnection _appServiceConnection ;
24
27
private BackgroundTaskDeferral _appServiceDeferral ;
25
28
@@ -37,45 +40,42 @@ protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
37
40
38
41
private async void OnAppServiceRequestReceived ( AppServiceConnection sender , AppServiceRequestReceivedEventArgs args )
39
42
{
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
+ }
43
50
44
- try
51
+ switch ( cmd )
45
52
{
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
+ }
50
59
51
60
Log . Comment ( "Received request for Page: {0}" , pageName ) ;
52
61
53
- ValueSet returnMessage = new ValueSet ( ) ;
54
-
55
62
// 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 ) ) ;
64
64
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 ;
78
74
}
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 ( ) ;
79
79
}
80
80
81
81
private void OnAppServicesCanceled ( IBackgroundTaskInstance sender , BackgroundTaskCancellationReason reason )
@@ -88,16 +88,38 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
88
88
_appServiceDeferral . Complete ( ) ;
89
89
}
90
90
91
- public async void SendLogMessage ( string level , string msg )
91
+ public async Task SendLogMessage ( string level , string msg )
92
92
{
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
+ } ;
97
99
98
100
await _appServiceConnection . SendMessageAsync ( message ) ;
99
101
100
102
// TODO: do we care if we have a problem here?
101
103
}
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
+ }
102
124
}
103
125
}
0 commit comments