none
NavigationService.Navigate is nothing

    Question

  • Hi,

    I am newbie of Silverlight.

    I am try to navigate from Page1.xaml to Page2.xaml by statement

    NavigationService.Navigate(New Uri("/Page2.xaml", UriKind.Relative))

    it said NavigationService is nothing

    Anyone Could help me?

    Thank you!

    Francis SZE

    Wednesday, May 02, 2012 10:10 PM

Answers

  • Hi ,

    Without using a Frame , you cant use NavigationService .  

    Alternatively, instead of using NavigationService then,  you could just write    this.Content = new Page2();    in that button click event, which will also take you to your Page2. 

    Hope it Helps Laughing

    Thursday, May 03, 2012 1:26 AM

All replies

  • Hi ,

       If you want to use the Navigation service, you can use Frame to load the Page1 , and from then onwards, you will not have any problem  using NavigationService.  If you dont use a frame, then you get Object reference not set exception for the NavigationService.

    Here is my Code anyway. 

    SilverlightControl1.xaml

    <UserControl x:Class="SilverlightApplication7.SilverlightControl1"
        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"
        d:DesignHeight="300" d:DesignWidth="400"
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <sdk:Frame Height="225" HorizontalAlignment="Left" Margin="25,43,0,0" Name="frame1" VerticalAlignment="Top" Width="338" />
        </Grid>
    </UserControl>
    

    SilverlightControl1.xaml.cs

    namespace SilverlightApplication7
    {
        public partial class SilverlightControl1 : UserControl
        {
            public SilverlightControl1()
            {
                InitializeComponent();
                frame1.Source = new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute);
            }
        }
    }

     

    Page1.xaml

    <navigation:Page x:Class="SilverlightApplication7.Page1" 
               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="Page1 Page">
        <Grid x:Name="LayoutRoot">
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="191,134,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </Grid>
    </navigation:Page>
    

     

    Page1.xaml.cs

    namespace SilverlightApplication7
    {
        public partial class Page1 : Page
        {
            public Page1()
            {
                InitializeComponent();
            }
    
            // Executes when the user navigates to this page.
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                NavigationService.Refresh();
                NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.RelativeOrAbsolute));
            }
    
        }
    }
    

     

    Page2.xaml

    <navigation:Page x:Class="SilverlightApplication7.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" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
        <Grid x:Name="LayoutRoot">
            <sdk:Label Height="28" HorizontalAlignment="Left" Content="This is page 2" Margin="156,129,0,0" Name="label1" VerticalAlignment="Top" Width="120" />
        </Grid>
    </navigation:Page>
    

     It is working on my machine.    Hope it Helps Laughing 

    Thursday, May 03, 2012 12:00 AM
  • Hi Phani Kiran Challa,

    I don't want to use frame.

    But goto page2.xaml by NavigationService.Navigate(New Uri("/Page2.xaml", UriKind.Relative)) on button_onclick event...

    a way can do this?

    Thank you!

    Thursday, May 03, 2012 12:32 AM
  • Hi ,

    Without using a Frame , you cant use NavigationService .  

    Alternatively, instead of using NavigationService then,  you could just write    this.Content = new Page2();    in that button click event, which will also take you to your Page2. 

    Hope it Helps Laughing

    Thursday, May 03, 2012 1:26 AM