@@ -195,8 +195,8 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
195195 {
196196 try
197197 {
198- var pageInstance = Activator . CreateInstance ( CurrentSample . PageType ) ;
199- SampleContent . Content = pageInstance ;
198+ SamplePage = Activator . CreateInstance ( CurrentSample . PageType ) as Page ;
199+ SampleContent . Content = SamplePage ;
200200
201201 // Some samples use the OnNavigatedTo and OnNavigatedFrom
202202 // Can't use Frame here because some samples depend on the current Frame
@@ -206,7 +206,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
206206
207207 if ( method != null )
208208 {
209- method . Invoke ( pageInstance , new object [ ] { e } ) ;
209+ method . Invoke ( SamplePage , new object [ ] { e } ) ;
210210 }
211211 }
212212 catch
@@ -348,6 +348,8 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
348348
349349 private void SamplePage_Loaded ( object sender , RoutedEventArgs e )
350350 {
351+ SamplePage . Loaded -= SamplePage_Loaded ;
352+
351353 if ( CurrentSample != null && CurrentSample . HasXAMLCode )
352354 {
353355 _lastRenderedProperties = true ;
@@ -516,17 +518,23 @@ private void UpdateXamlRender(string text)
516518 if ( CurrentSample . HasType )
517519 {
518520 root = SamplePage ? . FindDescendantByName ( "XamlRoot" ) ;
519- }
520521
521- if ( root is Panel )
522- {
523- // If we've defined a 'XamlRoot' element to host us as a panel, use that.
524- ( root as Panel ) . Children . Clear ( ) ;
525- ( root as Panel ) . Children . Add ( element ) ;
522+ if ( root is Panel )
523+ {
524+ // If we've defined a 'XamlRoot' element to host us as a panel, use that.
525+ ( root as Panel ) . Children . Clear ( ) ;
526+ ( root as Panel ) . Children . Add ( element ) ;
527+ }
528+ else
529+ {
530+ // if we didn't find a XamlRoot host, then we replace the entire content of
531+ // the provided sample page with the XAML.
532+ SamplePage . Content = element ;
533+ }
526534 }
527535 else
528536 {
529- // Otherwise, just replace the entire page 's content
537+ // Otherwise, just replace our entire presenter 's content
530538 SampleContent . Content = element ;
531539 }
532540
@@ -675,7 +683,8 @@ public bool UseBackground
675683 }
676684 }
677685
678- private Page SamplePage => SampleContent . Content as Page ;
686+ // The Loaded Instance of the backing .xaml.cs Page (if any)
687+ private Page SamplePage { get ; set ; }
679688
680689 private bool CanChangePaneState => ! _onlyDocumentation ;
681690
0 commit comments