{"id":6040,"date":"2014-04-12T02:53:29","date_gmt":"2014-04-12T02:53:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/how-to-reference-xaml-objects-in-code-behind-of-another-page-collection-of-common-programming-errors-2\/"},"modified":"2014-04-12T02:53:29","modified_gmt":"2014-04-12T02:53:29","slug":"how-to-reference-xaml-objects-in-code-behind-of-another-page-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/how-to-reference-xaml-objects-in-code-behind-of-another-page-collection-of-common-programming-errors-2\/","title":{"rendered":"How to reference XAML objects in code-behind of another page-Collection of common programming errors"},"content":{"rendered":"<p>App.xaml.cs cannot use the NavigationService directly because it is an application object, not a page object. However, it can invoke NavigationService through the RootFrame (i.e., the current page):<\/p>\n<table>\n<tr>\n<td>RootFrame.Navigate(new\u00a0Uri(&#8220;\/NextPage.xaml&#8221;,\u00a0UriKind.Relative));\u00a0<\/td>\n<\/tr>\n<\/table>\n<p>If you want to pass a string between App.xaml.cs and the page, you can define a public static string variable that can be accessed from both classes.<\/p>\n<p>However, I have a feeling that the tutorial code example isn&#8217;t the best model for what you want to do.<\/p>\n<p>The code example shows how to create global ApplicationBars in App.xaml that can be reused in different pages in your application. If you only need to use those appbars in one Pivot page, it would be easier to define them on locally that page instead of globally in App.xaml.\u00a0Doing it this way would eliminate the issues you encountered.\u00a0The article actually mentions it briefly: &#8220;<em>For the purposes of this example, you create two global Application Bars by using XAML in App.xaml. In your applications, you can also create global Application Bars by using only code in the App.xaml code-behind file, <strong>or local Application Bars in the code-behind file of your pivot page<\/strong><\/em>&#8220;.<\/p>\n<p>To put the ApplicationBars in your Pivot page instead of App.xaml, modify the example as follows:<\/p>\n<ol>\n<li>Instead of putting the ApplicationBars in in App.xaml, put them in in your Pivot page xaml:<br \/>\n<table>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>Put all the click handlers in your Pivot page code behind, instead of App.xaml.cs<\/li>\n<li>In the Pivot&#8217;s SelectionChanged handler, change it to access the local ApplicationBar, not global ones in App.xaml:<br \/>\n<table>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private\u00a0void\u00a0Pivot_SelectionChanged(object\u00a0sender,\u00a0SelectionChangedEventArgs\u00a0e)\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0switch\u00a0(((Pivot)sender).SelectedIndex)\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case\u00a00:\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ApplicationBar\u00a0=\u00a0((ApplicationBar)this.Resources[&#8220;AppBar1&#8221;]);\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case\u00a01:\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ApplicationBar\u00a0=\u00a0((ApplicationBar)this.Resources[&#8220;AppBar2&#8221;]);\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<\/ol>\n<p>Richard Woo<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App.xaml.cs cannot use the NavigationService directly because it is an application object, not a page object. However, it can invoke NavigationService through the RootFrame (i.e., the current page): RootFrame.Navigate(new\u00a0Uri(&#8220;\/NextPage.xaml&#8221;,\u00a0UriKind.Relative));\u00a0 If you want to pass a string between App.xaml.cs and the page, you can define a public static string variable that can be accessed from both [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6040","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6040"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6040\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}