Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 27 March 2009

Silverlight 3 : Master Pages implementation with Navigation Framework – Part 4

Posted on 06:45 by Unknown

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>

One thing you must have notice that, Instead of Buttons in old code, now I am making use of Hyperlink button to give exact feel of navigation.Observe carefully that to send request to particular page, you need to set “Tag” property as path or url to the target page. I have added few pages as per requirement in a separate folder Content in my project. To add Separate page, you can do so by right clicking on Silverlight project, then Add New Item, there you will find a separate template for user control and separate template for page, like this :

slmp4
Now understand C# Code :
using System;
using System.Windows;
using System.Windows.Controls;

namespace SL_NavigationDemo
{

public partial class MainPage : UserControl
{

public MainPage()
{
InitializeComponent();
}

private void Redirect_Click(object sender, RoutedEventArgs e)

{
HyperlinkButton hybtn = sender as HyperlinkButton;

string refurl = hybtn.Tag.ToString();

this.MainMaster.Navigate(new Uri(refurl, UriKind.Relative));

}

}

}

The Redirect_Click event we have already associated with each HyperlinkButton Click in xaml code, Here we are just casting it and passing the “Tag” to Navigate() which is method provided by Silverlight 3 Navigation Framework which accepts Relative URL and redirects to the corresponding page. That’s all you need to do in this to implement Master pages kind of functionality.If you are planning to make some default page while navigation, you need to set “Source” property of Navigate tag, Navigate Tag provides two options by default, one is “Frame” and another one is “Page”. If you run the application, you can see change in URL, previously in Silverlight 2 and below versions, it was not showing anything besides “aspx” or “html” in address bar, but now from Silverlight 3 we have navigation support across pages, URLs look like following :


slmp2

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.

Now, some of you may raise your eye,since URL is getting expose directly in address bar. so there are few things which if we implement, we can hide that exposing address. What we need to do is set those Tag urls under App.xaml as global resource like this :

<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"/>



So now URL will look like :

slmp3

And final Output will look like :

slmp1

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.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ▼  March (9)
      • Your Chance to Become Microsoft Most Valuable Prof...
      • Silverlight 3 : Master Pages implementation with N...
      • Silverlight 3 : DataForm control features – Part 3
      • Silverlight 3 : Explore Power of Blend 3 Preview –...
      • Silverlight 3 : Lap around Silverlight 3 Beta 1 :...
      • Silverlight 3 and Blend 3 is finally here…!!!
      • Start Windows…
      • 3D in Silverlight 2 with Kit3D
      • Lap Around ASP.NET Dynamic Data 4.0 Preview
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile