12
12
using Windows . UI . Xaml ;
13
13
using Windows . UI . Xaml . Controls ;
14
14
using Windows . UI . Xaml . Input ;
15
+ using Windows . UI . Xaml . Media . Animation ;
15
16
16
17
namespace UITests . App
17
18
{
@@ -24,28 +25,37 @@ public sealed partial class MainTestHost
24
25
25
26
private Assembly _executingAssembly = Assembly . GetExecutingAssembly ( ) ;
26
27
28
+ private TaskCompletionSource < bool > _loadingStateTask ;
29
+
27
30
public MainTestHost ( )
28
31
{
29
32
InitializeComponent ( ) ;
30
33
( ( App ) Application . Current ) . host = this ;
31
34
_queue = DispatcherQueue . GetForCurrentThread ( ) ;
32
35
}
33
36
34
- internal bool OpenPage ( string pageName )
37
+ // TODO: we should better expose how to control the MainTestHost vs. making this internal.
38
+ internal async Task < bool > OpenPage ( string pageName )
35
39
{
36
40
try
37
41
{
38
42
Log . Comment ( "Trying to Load Page: " + pageName ) ;
39
43
44
+ _loadingStateTask = new TaskCompletionSource < bool > ( ) ;
45
+
40
46
// Ensure we're on the UI thread as we'll be called from the AppService now.
41
- _queue . EnqueueAsync ( ( ) =>
47
+ _ = _queue . EnqueueAsync ( ( ) =>
42
48
{
43
- navigationFrame . Navigate ( FindPageType ( pageName ) ) ;
49
+ // Navigate without extra animations
50
+ navigationFrame . Navigate ( FindPageType ( pageName ) , new SuppressNavigationTransitionInfo ( ) ) ;
44
51
} ) ;
52
+
53
+ // Wait for load to complete
54
+ await _loadingStateTask . Task ;
45
55
}
46
56
catch ( Exception e )
47
57
{
48
- Log . Error ( "Exception Finding Page {0}: {1} " , pageName , e . Message ) ;
58
+ Log . Error ( "Exception Loading Page {0}: {1} " , pageName , e . Message ) ;
49
59
return false ;
50
60
}
51
61
@@ -69,11 +79,34 @@ private Type FindPageType(string pageName)
69
79
private void NavigationFrame_Navigated ( object sender , Windows . UI . Xaml . Navigation . NavigationEventArgs e )
70
80
{
71
81
Log . Comment ( "Navigated to Page {0}" , e . SourcePageType . FullName ) ;
82
+ if ( e . Content is Page page )
83
+ {
84
+ if ( page . IsLoaded )
85
+ {
86
+ Log . Comment ( "Loaded Page {0}" , e . SourcePageType . FullName ) ;
87
+ _loadingStateTask . SetResult ( true ) ;
88
+ }
89
+ else
90
+ {
91
+ page . Loaded += this . Page_Loaded ;
92
+ }
93
+ }
94
+ }
95
+
96
+ private void Page_Loaded ( object sender , RoutedEventArgs e )
97
+ {
98
+ var page = sender as Page ;
99
+
100
+ page . Loaded -= Page_Loaded ;
101
+
102
+ Log . Comment ( "Loaded Page (E) {0}" , page . GetType ( ) . FullName ) ;
103
+ _loadingStateTask . SetResult ( true ) ;
72
104
}
73
105
74
106
private void NavigationFrame_NavigationFailed ( object sender , Windows . UI . Xaml . Navigation . NavigationFailedEventArgs e )
75
107
{
76
108
Log . Error ( "Failed to navigate to page {0}" , e . SourcePageType . FullName ) ;
109
+ _loadingStateTask . SetResult ( false ) ;
77
110
}
78
111
}
79
112
}
0 commit comments