Skip to main content
Home Forums General Silverlight New Features in Silverlight 3 Why is NavigationService null?
4 replies. Latest Post by Marie123 on November 6, 2009.
(0)
Marie123
Member
3 points
11 Posts
11-05-2009 3:37 AM |
I can't seem to do a simple navigation using the NavigationService it's always null. Initially I converted thru wild guesses my UserControl based xaml to page it loads OK but the darn nagivation hit the null exception, please don't tell me use frame I don't understand all this i and u frame nesting :). I wanna go to a different (aspx) page perhaps w/ a different sl.xap or the same.
Anyway, to make sure I did not do something wrong with my original page. I started a brand new Page2 from the templates.
Here is the code
namespace AMS { public partial class Page2 : Page { public Page2() { InitializeComponent(); } // Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { } private void PagePrevious_Click(object sender, RoutedEventArgs e) { if (this.NavigationService.CanGoBack) { this.NavigationService.GoBack(); } } } }
Here is the xaml
<navigation:Page x:Class="AMS.Page2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" d:DesignWidth="640" d:DesignHeight="480" Title="Page2 Page"> <Grid x:Name="LayoutRoot"> <Button x:Name="PagePrevious" Content="Back" VerticalAlignment="Top" Margin="0,17,18,0" ToolTipService.ToolTip="Show log" HorizontalAlignment="Right" Grid.Column="2" Click="PagePrevious_Click"/> </Grid> </navigation:Page>
made page2 the startup page
1 private void Application_Startup(object sender, StartupEventArgs e) 2 { 3 this.RootVisual = new Page2(); 4 Page2 Pg = (Page2)this.RootVisual; 5 }
Please tell me what am I doing wrong.
I've visited browsed/read and bookmarked a zillion navigaton framework bla-bla blogs most showing hand crafting the links to home/about/etc pages, or using the uri_mapper. I have not hit on one which provides a simple, working example, like navigate to this page. Most importantly none mention what causes navigationservice to be instantiated reference instead always being null.
Maybe I am going about it the wrong way, but for now I have one .xap I host in very similar .aspx pages and want my app to offer a back button to parent pages (actually eventually specified via an xml resource) for now GoBack is all I am asking for, I believe that's not much :(
This has been 2 frustrating days just around a simple navigation.
Thanks for any help.
Giftednewt
355 points
64 Posts
11-05-2009 5:29 AM |
It sounds as if you are trying to use the navigation framework for something it was not intended to be used for.
Correct me if I'm wrong, but AFAIK the navigation framework is meant to be used to allow navigation of pages within a single silverlight application. It also allows the ability to deep-link and the use of the browser back/forward navigation within the silverlight application.
The reason that the NavigationService is null on your page is that you need a "Main" UserControl with a navigation frame on it that is used to display the pages you are navigating between inside the silverlight application. Once you navigate the navigation frame to a URI you have mapped that page will have a reference to the NavigationService.
If all you wan to do is go back or forward with the browser outside of the silverlight application you may need to create a couple of javascript functions on your page and call them using the Html bridge.
11-05-2009 11:46 AM |
Thanks for the reply! You are right about the navigation framework. Believe me, I do want to take advantage of it as I grow my knowledge of this painful SL stuff and also grow my app(s) as well. For now my web/SL strategy is simple (dumb) I navigate to different aspx/html pages each in different folders and each having a sibling XML the sl.xap resolves to.
After posting I realized I could use a transparent hyperlinkbutton overlayed over a std. button to provide my return to parent navigation or others. [BTW, the click does not tunnel to the button, through, some folks and I included might find it useful despite everyone else thinking why would you ever need to. For example, I might want the button to animate underneath the hotspot on click. Give the darn control to the programmer please!!!]. I was already using an instance of the hyerplinkbutton to navigate to the linked pages. I don't know how I embarked on this navigation framework stuff :)
Oh yeah! I recall now, I don't understand why button and image don't come with an URI prop, yah yah!, keep it light. Ok, I still need just direct programmatic way to navigate somewhere, say based, on an event. The hyperlinkbutton does not provide a method like NavigateTo, how dumb!?. I could use a hidden one to do my navigations. Please MS give me a simple API not a Phd dissertation. Why does Microsoft, always give me the least? Here is a quick list:
Back to the beef. Can you show me (post please) how to wrap the sample xaml I provided with a "Main" UserControl and navigation frame so NavigationService has a valid reference? Are you saying I need a separate root page? Please guide me I am totally blind at the moment.
Thanks for all the help
11-06-2009 9:21 AM |
Create a Usercontrol with Xaml similar to the following and assign an instance of it as the RootVisual
<UserControl x:Class="SilverlightApplication1.NavPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White"> <navigation:Frame x:Name="ContentFrame" Source="/ContentPage"> <navigation:Frame.UriMapper> <uriMapper:UriMapper> <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/{pageName}.xaml"/> </uriMapper:UriMapper> </navigation:Frame.UriMapper> </navigation:Frame> </Grid> </UserControl>
11-06-2009 12:11 PM |
So essentially this page-hosting control becomes the container for all pages within the SL app. I think I am starting to understand a bit :)
Therefor, the moment I travel to another aspx/sl2.xap I will be dealing with a totally different instance of NavigationService, right?
No connection what so ever with the previous NavigationService. Of course, each could have its own set of xaml-pages.
Note if I want to have my common back button in this container rather than in the xaml/pages traveled to it would have to derive from page and not usercontrol because usercontrol does not seem to have NavigationService.
Thank you . Giftednewt!