Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

silverlight page switch

I am using silverlight and I am unsure about a better solution to my problem. I couldnt download a working example of this
http://www.silverlight.net/learn/videos/all/navigation-framework/

I have a game with say 2 levels and each level has a number of images.
I have a screen menu.

My current solution  is
I load the first level on mainpage.xaml.vb and when I goto the next level I erase all screen objects from level1 and then add all the level2 objects on the screen. So I am reusing the same page with different classes and images etc.
A screen menu was just a popup image I am using over the current level.

My desired solution is
What I wanted was to load the level 1 objects on page1.xaml.vb and level 2 objects on page2.xaml.vb with classes etc.
Switching levels would be to just signal what page to display and unload the other via another a mainPage.
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

In the demo, he did not use Silverlight Navigation project but he added navigation functionality from scratch.

To make things easy, MS has a project template for Silverlight Navigation. This project provides basic infrastructure needed. Just check on that and you would get a direction to start on.

Not sure if this project was there with SL3 but it is there with SL4
Avatar of jagguy

ASKER

>>Silverlight Navigation. This project provides basic infrastructure needed. Just check on that and you >>would get a direction to start on.

OK I know the navigation template and without going into details I am not getting anywhere fast. The information supplied is confusing and examples on the web out dated. I cant find a basic example of page switching like my basic  example above so that is why i am asking here.
Avatar of jagguy

ASKER

the example given is silverlight 2 and I believe silverlight 3 which I have is different again
It also works in Silverlight 3.0.

To do this in new Page Navigation, watch video at
http://www.silverlight.net/learn/videos/silverlight-videos/navigation-framework/

HTH
Ashok
Avatar of jagguy

ASKER

I saw the video and downloaded the sample code and it doesnt work.
I am not sure if this is outdated or has errors.
Download may not be working, but you have to follow the video and
re-create the navigation application by following steps.

It will work with VS 2008 and Silverlight 3.0.

HTH
Ashok
Avatar of jagguy

ASKER

OK bets to work wit the code I have rather then link jumping for yet other ways of doing things. In my experience try a bit then ask for help otherwise you will waste time in google world.


Well I am getting an error on the bold liine in main.xaml. I have added page1.xaml and page2.xaml. I want to click a button in main and switch to a page1 or page2.

.Error    1    The tag 'Frame' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'.    D:\silverlight\nav1\nav1\MainPage.xaml    13    10    nav1
Error    2    The type 'ResourceDictionary' is inside a ResourceDictionary and does not have a key.    D:\silverlight\nav1\nav1\App.xaml    nav1


main.xaml

<UserControl
    x:Class="nav1.MainPage"
    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"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <Grid x:Name="LayoutRoot" Margin="0">

        <sdk:Frame x:Name="Page1" UriMapper="{StaticResource GameUriMapper}"/>

    </Grid>

</UserControl>

app.xaml


<Application  
  x:Class="nav1.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
    
    >
    <Application.Resources>
        <navigation:UriMapper x:Key="GameUriMapper">

            <navigation:UriMapping Uri="{}{page}" MappedUri="/{page}.xaml"/>

        </navigation:UriMapper>



        <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Assets/Styles.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

</Application>


main.xaml.vb

   Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        bt.Width = 200
        bt.Height = 100
        AddHandler bt.Click, AddressOf bt_click

    End Sub

    Public Sub bt_click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        PageFrame.Navigate(New Uri("Level1", UriKind.Relative))
    End Sub

Open in new window

Looks like you are using the wrong namespace. This is the namespace used in one of the articles above and i have attached the code from main.xaml

xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"


<UserControl x:Class="SilverlightApplication34.MainPage"
    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:local="clr-namespace:SilverlightApplication34">
    <Grid x:Name="LayoutRoot" Background="{StaticResource ApplicationBackgroundColorBrush}">
        <Grid.Resources>
            <local:VisibilityBooleanConverter x:Key="VisBool" />
        </Grid.Resources>

            <Grid Style="{StaticResource NavigationContainerStyle}">

            <Border Style="{StaticResource NavigationBorderStyle}">
            
                <StackPanel Style="{StaticResource NavigationPanelStyle}">

                    <Button Click="NavButton_Click" Tag="/Views/HomePage.xaml" Content="home" 
                            Style="{StaticResource PageLinkStyle}"/>
                    <Button Click="NavButton_Click" Tag="/Views/AboutPage.xaml" Content="about" 
                            Style="{StaticResource PageLinkStyle}"/>
                            
                            <Button Click="NavButton_Click" Tag="/Views/SubNavigation.xaml" Content="sub navigation" 
                            Style="{StaticResource PageLinkStyle}"/>
                
                </StackPanel>
            
            </Border>
            
            <Border Style="{StaticResource BrandingBorderStyle}">
            
                <StackPanel Style="{StaticResource BrandingPanelStyle}" x:Name="JournalNav">
                
                    <Button Content="back" Style="{StaticResource PageLinkStyle}" x:Name="NavBack" Click="NavBack_Click" />
                    <Button Content="forward" Style="{StaticResource PageLinkStyle}" x:Name="NavFwd" Click="NavFwd_Click" />

                </StackPanel>
                
            </Border>
            
        </Grid>

        <Border Style="{StaticResource FrameContainerStyle}">

            <Border Style="{StaticResource FrameInnerBorderStyle}">

                <navigation:Frame Navigated="Frame_Navigated" x:Name="Frame" Source="/Views/HomePage.xaml"
                                  HorizontalContentAlignment="Stretch"
                                  VerticalContentAlignment="Stretch"
                                  Padding="15,10,15,10"
                                  Background="White"/>

            </Border>

        </Border>

    </Grid>
</UserControl>

Open in new window

Avatar of jagguy

ASKER

no my name spaces are the same except for
  xmlns:local="clr-namespace:SilverlightApplication34"

which gives an error. did you try my code in VS2008?
No. I am not on my dev pc.

You are using <sdk:Frame where sdk refers to the namespace
"http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

whereas in the tutorial, <navigation:Frame is used and navigation refers to the namespace
"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Avatar of jagguy

ASKER

I dont know what to do here because this was default code.

Error      1      The type 'ResourceDictionary' is inside a ResourceDictionary and does not have a key.      D:\silverlight\nav1\nav1\App.xaml      nav1

<Application   
  x:Class="nav1.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
  
    >
    <Application.Resources>
        <navigation:UriMapper x:Key="GameUriMapper">

            <navigation:UriMapping Uri="{}{page}" MappedUri="/{page}.xaml"/>

        </navigation:UriMapper>



        <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Assets/Styles.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

</Application>

Open in new window

All I can say is go through the examples and try to build a simple small page first and if you succeed then implement it in your own app. You were supposed to be making changes in main.xaml not app.xaml I believe.
ASKER CERTIFIED SOLUTION
Avatar of jagguy
jagguy
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Open new question, I will post complete project for Silverlight 3.0 with Page Navigation.

Ashok