First of all, I wish “Happy New Year” to all my blog readers from India on occasion of “Gudhipadwa-Hindu New Year Day”.
After Introductory articles on Silverlight 3 which you can see as Part1, Part2 and Part3. Now I am going for Part 4.
Few months back I wrote an article on Implementing Master pages in Silverlight 2 , and I got very good response and feedback from all of you, that time I made use of Stack Panel and some small code logic which instructs to load different content dynamically. With Silverlight 2, unfortunately we had no navigation support or framework as such, and we use to do it with such small tricks and workaround.
Let me now make you aware of Navigation Framework in Silverlight 3 with which I am going to rebuild my Master Pages application in Silverlight 3 which I had developed in Silverlight 2. For this, I am making use of some code methodology and concepts shown in screencast by Tim Heuer in his screencast. Using that I am going to build the new code for Master Page implementation with some tweaks in UI Code which I build in past for the same.
Firstly, Create a new Silverlight Project and set reference to following :
- System.Windows.Controls
- System.Windows.Controls.Navigation
There is one “Silverlight Navigation Application” template also, but I am not going to talk about it right now, but surely I am going to touch upon that soon.
Well, Most of the XAML code of my last Master Page article I am keeping the same, with new minor changes, new code is :
<Grid x:Name="LayoutRoot" Background="AliceBlue">
<HyperlinkButton x:Name="btnBlog" Click="Redirect_Click" Tag="/Content/Blog.xaml" Height="22" Width="67" Content="Blog" Margin="161.5,36,171.5,0" VerticalAlignment="Top" />
<HyperlinkButton x:Name="btnRSS" Click="Redirect_Click" Tag="/Content/RSS.xaml" Height="22" Width="67" Content="RSS" HorizontalAlignment="Left" Margin="94,36,0,0" VerticalAlignment="Top"/>
<HyperlinkButton x:Name="btnHome" Click="Redirect_Click" Tag="/Content/Home.xaml" Height="22" Width="67" Content="Home" HorizontalAlignment="Left" Margin="27,36,0,0" VerticalAlignment="Top"/>
<HyperlinkButton x:Name="btnDownload" Click="Redirect_Click" Tag="/Content/Download.xaml" Height="22" Width="67" Content="Download" Margin="0,36,104,0" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<HyperlinkButton x:Name="btnAboutUs" Click="Redirect_Click" Tag="/Content/AboutUs.xaml" Height="22" Width="67" Content="About Us" Margin="0,36,20,0" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock Height="23" Margin="94,8,116,0" VerticalAlignment="Top" Text="Explore .NET with Vikram Pendse" TextWrapping="Wrap" Foreground="#FF000000"/>
<StackPanel x:Name="stk" Width="400" Margin="0,66,0,30">
<navigate:Frame x:Name="MainMaster"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"/>
</StackPanel>
<TextBlock Height="23" Margin="106,0,104,3" VerticalAlignment="Bottom" Text="Microsoft .NET for everyone!!" TextWrapping="Wrap" Foreground="#FF000000"/>
</Grid>
Now understand C# Code :
using System;using System.Windows;
using System.Windows.Controls;
namespace SL_NavigationDemo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
{
HyperlinkButton hybtn = sender as HyperlinkButton;
string refurl = hybtn.Tag.ToString();
}
}
}
It shows something..aspx#/Content/Blog.xaml, so it shows which XAML page which you are accessing currently, Also make a point that Browser’s Title is not set automatically, It has been set through individual page, let me drop few lines of XAML of Blogs.xaml page, so that you can get some idea.
<navigation:Page x:Class="SL_NavigationDemo.Content.Blog" 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" Title="Blog Page">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="You are on Blog Page" FontSize="20"/>
</Grid>
</navigation:Page>
Observe carefully now, Its now no longer <navigate:Frame> but its <navigate:Page>, This will render inside frame which we have on MainPage.xaml, “Title” attribute helps to set-reset Title of page, This is just like <title> tag in HTML. Inside grid we can put whatever we want to. Each separate XAML Page will act as content page in our Master page implementation scenario.
<Application.Resources>
<navigate:UriMapper x:Key="uriMapper">
<navigate:UriMapping Uri="Home" MappedUri="/Content/Home.xaml"/>
<navigate:UriMapping Uri="Blog" MappedUri="/Content/Blog.xaml"/>
<navigate:UriMapping Uri="RSS" MappedUri="/Content/RSS.xaml"/>
<navigate:UriMapping Uri="Download" MappedUri="/Content/Download.xaml"/>
<navigate:UriMapping Uri="AboutUs" MappedUri="/Content/AboutUs.xaml"/>
</navigate:UriMapper>
</Application.Resources>
UriMapper will help us to set the URL which we are targeting and MappedUri can be set with the target URL, make note that whatever “Uri” we setting here, same content to be set in place of “Tag” attribute of HyperlinkButton, else application will throw runtime exception. So finally MainPage.xaml’s HyperlinkButton block will look like this :
<HyperlinkButton Click="Redirect_Click" Tag="Blog" Height="22" Width="67" Content="Blog" Margin="161.5,36,171.5,0" VerticalAlignment="Top" />
<HyperlinkButton Click="Redirect_Click" Tag="RSS" Height="22" Width="67" Content="RSS" HorizontalAlignment="Left" Margin="94,36,0,0" VerticalAlignment="Top"/>
<HyperlinkButton Click="Redirect_Click" Tag="Home" Height="22" Width="67" Content="Home" HorizontalAlignment="Left" Margin="27,36,0,0" VerticalAlignment="Top"/>
<HyperlinkButton Click="Redirect_Click" Tag="Download" Height="22" Width="67" Content="Download" Margin="0,36,104,0" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<HyperlinkButton Click="Redirect_Click" Tag="AboutUs" Height="22" Width="67" Content="About Us" Margin="0,36,20,0" VerticalAlignment="Top" HorizontalAlignment="Right"/>
Hope this example have given you some good introduction of Navigation Framework in Silverlight 3 Beta. I know, something you might be expecting more from me, well as I told in my previous articles, This is just lap around, So I am surely covering each of these features in great details in coming days. Also soon I will be sharing “Deep Linking” with you.Meanwhile I am really looking ahead for your feedback and suggestions,so keep visiting here for more stuff on Silverlight 3.
Vikram.
4 comments:
Hi,
I'd like to ask you this: each time I click on an hyperlink, the masterpage is "reloaded"?
I'm developing a large application, with a menu on top and a lot of page of content. Cause the number of pages is very large, I'd like to ask you if it is possible to generate different .xap files, one for each "set of pages". If I put the masterpage in one of these xap files, what is happen when i change xap file and then i return on this?
Thank you very much
Hello decima_comunita,
Creating diff. xap files will might be extra work and think upon the scenario about downloading those set of xaps at client end, may indirectly create impact on performance.
You can instead do this by creating separate xaml pages like I have shown over here in my article, since you have well support of Navigation Framework,So keep one Master Page and load rest of the page by means of Navigation Framework provided by Silverlight 3 Beta. Let me know if you have any more issues or if solution is not suits to your scenario.
hi,
When I copy-pasted the XAML code in the Maingpage.xaml , after adding the references to those assemblies ,it still shows that navigate namespace not found.
Please help.
Make correction in your code, Use Navigation: Frame instead of Navigate:frame.
Thanks.
Post a Comment