sharpcorner.com/uploadfile/37db1d/4958/default.aspx?articleid=cb0b291c-52ae-4b80-a95c- 438d76fa1145

Similar documents
RadPDFViewer For Silverlight and WPF

Lab 7: Silverlight API

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C5: The Complete Code of PathAnimation. Copyright by V. Miszalok, last update:

CS3240 Human-Computer Interaction Lab Sheet Lab Session 3 Designer & Developer Collaboration

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C1: The Intro Project

CPSC Tutorial 5 WPF Applications

windows-10-universal #windows- 10-universal

Nebraska - eforms. Tips and Tricks

WebFront for Service Manager

Week 7: NavigationView Control Exercise

Authoring Guide Gridpro AB Rev: Published: March 2014

Introduction to Data Templates and Value Converters in Silverlight

Authoring Guide v2.1 PATRIK SUNDQVIST

CPSC Tutorial 5

Launchers and Choosers Hands-on Lab. Hands-On Lab. Launchers and Choosers. Lab version: Last updated: 12/8/2010. Page 1

Step4: Now, Drag and drop the Textbox, Button and Text block from the Toolbox.

Hands-On Lab. Using Pivot and Panorama Controls

Hands-On Lab. Hello Windows Phone

Week 6: First XAML Control Exercise

Windows Presentation Foundation. Jim Fawcett CSE687 Object Oriented Design Spring 2018

CS3240 Human-Computer Interaction Lab Sheet Lab Session 2

ComponentOne. HyperPanel for WPF

Note: This demo app created for this lab uses the Visual Studio 2015 RTM and Windows Tools SDK ver

Note: many examples in this section taken or adapted from Pro WPF 4.5 C#, Matthew MacDonald, apress, 2012, pp

RadGanttView For Silverlight and WPF

Lesson 9: Exercise: Tip Calculator

Advanced Programming C# Lecture 3. dr inż. Małgorzata Janik

Name of Experiment: Student Database

Workspace Desktop Edition Developer's Guide. Customize Views and Regions

Weather forecast ( part 2 )

CS3240 Human-Computer Interaction Lab Sheet Lab Session 5 Navigation Framework

Week 8: Data Binding Exercise (Bookstore)

Pro Business Applications with Silverlight 4

Portable Class Libraries ---

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Exam sell. Higher Quality Better Service! Certified IT practice exam authority.

CS3240 Human-Computer Interaction Lab Sheet Lab Session 4 Media, Ink, & Deep Zoom

Name of Experiment: Country Database

Yes, this is still a listbox!

Master Code on Innovation and Inclusion

Universal Windows Platform Complete Solution

UWP Working with Navigation

Migrating to Windows Phone

Hands-On Lab. Windows Phone Navigation and Controls

IAP C# Lecture 5 XAML and the Windows Presentation Foundation. Geza Kovacs

This book was purchased by

Hands-On Lab. Building Applications in Silverlight 4 Module 6: Printing the Schedule. Printing the Schedule

Nauticom NetEditor: A How-to Guide

Fundamentals of XAML and Microsoft Expression Blend

FOREWORD WHAT WE CAN LEARN TARGET READER

CPSC Tutorial 6

DOT.NET MODULE 6: SILVERLIGHT

Advance Windows Phone Development. Akber Alwani Window Phone 7 Development EP.NET Professionals User Group

ComponentOne. Extended Library for UWP

Chromatic Remote Control Product Guide Executive Way, Suite A Frederick, MD 21704

WRITING THE MANAGEMENT SYSTEM APPLICATION

Telerik Corp. Test Studio Standalone & Visual Studio Plug-In Quick-Start Guide

Creation of the application based on the DevForce Silverlight by IdeaBlade with the use of Report Sharp-Shooter for Silverlight by Perpetuum Software.

S AMPLE CHAPTER IN ACTION. Timothy Binkley-Jones Massimo Perga Michael Sync MANNING

ArcGIS Pro SDK for.net UI Design for Accessibility. Charles Macleod

CHANNEL9 S WINDOWS PHONE 8.1 DEVELOPMENT FOR ABSOLUTE BEGINNERS

CS3240 Human-Computer Interaction

Connecting a Silverlight Application to Line-of-Business Information

Define the Slide Animation Direction on the deck control.

Dreamweaver Basics Outline

How to set up a local root folder and site structure

Accurate study guides, High passing rate! IT TEST BOOK QUESTION & ANSWER. Ittestbook provides update free of charge in one year!

Applied WPF 4 in Context US $49.99 SOURCE CODE ONLINE

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) More on Relative Linking. Learning Objectives (2 of 2)

Microsoft Corporation

Developing Native Windows Phone 7 Applications for SharePoint

Quick Guide for the ServoWorks.NET API 2010/7/13

Topics. Windows Phone

NE Fundamentals of XAML and Microsoft Expression Blend

Index. Windows 10 running, 199 suspended state, 199 terminate apps,

Introduction. Part I: Silverlight Fundamentals for ASP.NET Developers 1

Introducing the Visual Studio Extensibility

Project 1: Creating a Web Site from Scratch. Skills and Tools: Use Expression Web tools to create a Web site

10267A CS: Developing Web Applications Using Microsoft Visual Studio 2010

Architectural Engineering Senior Thesis CPEP Webpage Guidelines and Instructions

This tutorial is designed for software developers who want to learn how to develop quality applications with clean structure of code.

Interface Builders and Interface Description Languages

[MS10553]: Fundamentals of XAML and Microsoft Expression Blend

Microsoft Visual Studio 2010

Mastering LOB Development

CST272 Getting Started Page 1

EDITOR GUIDE. Button Functions:...2 Inserting Text...4 Inserting Pictures...4 Inserting Tables...8 Inserting Styles...9

Technology to put your digital fabric printing business online. User Manual


SHAREPOINT 2013 DEVELOPMENT

Hands-On Lab. Sensors -.NET. Lab version: Last updated: 12/3/2010

Using Dreamweaver CC. Logo. 4 Creating a Template. Page Heading. Page content in this area. About Us Gallery Ordering Contact Us Links

User Interface Changes for SYSPRO

Creating Web Pages with SeaMonkey Composer

Creating a Website in Schoolwires Technology Integration Center

Beginning Silverlight 5 in C #

This walkthrough assumes you have completed the Getting Started walkthrough and the first lift and shift walkthrough.

Programming in C# Project 1:

Page Layout Using Tables

Table Basics. The structure of an table

Transcription:

Navigation in Silverlight -3 1. Introduction: In previous article we learn to navigate to another Silverlight page without using navigation framework, which is new feature in Silverlight 3. Read it Here: http://www.c- sharpcorner.com/uploadfile/37db1d/4958/default.aspx?articleid=cb0b291c-52ae-4b80-a95c- 438d76fa1145 So in this article we will learn how to create a navigation experience similar to browsing through different pages of a web site. Navigation framework also allows us to create a history that can integrate with the browser enabling users to navigate forward and backward through the history using the browser s back and forward buttons. So before we dive deep into programming with navigation framework, we need to get to know two main objects that are contained in the Navigation Framework, Page and Frame. The Frame object is very similar to a ContentPlaceHolder in ASP.NET master pages and is the place holder for the different views to be loaded onto the page. 2. Creating a Silverlight Navigation Application. Step 1: Start Visual Studio 2008 and select File New Project from the main menu. Step 2: When the New Silverlight Application dialog appears, select the default to host the Silverlight application in a new ASP.NET web application named NavigationFrmk.Web. Press OK to continue.

Step 3: By default the MainPage.xaml file will be created and opened for editing. We will start by editing that file. In the Grid definition, add ShowGridLines="True" so We can see how our cells are laid out. We can turn this property off later so our application is cleaner Step 4: Next we want to define the Grid cells. We will simply have two rows, one for the links and one for the navigated content. <Grid ShowGridLines="True" x:name="lawetroot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition></RowDefinition> </Grid.RowDefinitions> </Grid> Step 5: Now that We have the two rows, We want to add our HyperlinkButtons that will be used to navigate to the different views. We will do this in a horizontal StackPanel. For the Click property, create an event handler called LinkClick. <Grid ShowGridLines="True" x:name="layoutroot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <HyperlinkButton Content="View 1" Click="HyperlinkButton_Click " Padding="5" /> <HyperlinkButton Content="View 2" Click="HyperlinkButton_Click" Padding="5" /> </StackPanel> </Grid> Step 6: The next step will be to add support for the Navigation Framework in our project. The first step is to add a reference to System.Windows.Controls. Navigation.dll by right clicking on the References folder in our Silverlight project and choosing Add Reference as shown below ;

Step 7: When the Add Reference dialog appears, be sure the.net tab is selected and then browse through the list until we find System.Windows.Controls.Navigation, as shown below. Select the entry and press OK to add the reference to the project. Step 8: When the assembly is added we will see it appear under References in the Solution Explorer, as shown below

Step 9: Now that you have added the reference to the Navigation Framework, we need to add the navigation objects to your application. You will start by adding the XML namespace for System.Windows.Controls.Navigation to the UserControl definition. Step 10: we can now add a Frame to the bottom row of the root grid named ContentFrame. You will also set the HorizontalContentAlignment and VerticalContentAlignment to Stretch so the Frame will consume the entire Grid Cell. we will also give the Frame a 10 pixel Margin and a BorderThickness to 2 pixels.

<navigation:frame x:name="contentframe" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="10" Grid.Row="1" BorderThickness="2" BorderBrush="Black" /> Step 11: Next, you will add the different views to the project. Right-click on the Silverlight project and select Add New Item. Step 12. On the Add New Item dialog, select the Silverlight Page template, name the page View1.xaml and click on the Add button.

Step 13: Once Page1.xaml has been added, we will repeat steps to add another Silverlight Page named Page2.xaml. Step 14: Open Page1.xaml up in design mode and add the following XAML to the root Grid. <Grid x:name="layoutroot"> <TextBlock Text="Page 1" FontSize="60" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> Step 15: Open Page2.xaml up in design mode and add the following XAML to the root Grid. <Grid x:name="layoutroot"> <TextBlock Text="Page 2" FontSize="60" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> Step 16: We now have the main page containing the Frame and the two pages that you will load into the Frame. Next, we need to actually load the pages into the Frame. We will do this on the click event of the two HyperlinkButtons. While we can easily do this with two click event handlers, we will actually do it with one. We can set the Tag property of the HyperlinkButton to be the page view source file. Then the click event handler will be able to retrieve the source file from the Tag. <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <HyperlinkButton Content="View 1"

Click="HyperlinkButton_Click" Padding="5" Tag="/Page1.xaml" > </HyperlinkButton> <HyperlinkButton Content="View 2" Click="HyperlinkButton_Click" Padding="5" Tag="/Page2.xaml"> </HyperlinkButton> </StackPanel> Step 17: Within the event we will add the following code to retrieve the view s source file. private void HyperlinkButton_Click(object sender, RoutedEventArgs e) { HyperlinkButton button = (HyperlinkButton)sender; string viewsource = button.tag.tostring(); } Step 18: Now that we have the view s source file, you can use the Frame s Navigate method to navigate to the proper view. private void HyperlinkButton_Click(object sender, RoutedEventArgs e) { HyperlinkButton button = (HyperlinkButton)sender; string viewsource = button.tag.tostring(); ContentFrame.Navigate(new Uri(viewSource, UriKind.Relative)); } Step 19 : We are now ready to run the solution. Select Debug Start Debugging or press F5 to run the application. Internet Explorer will open and the application will be displayed, as shown below,

Step 20: Press the View 1 HyperlinkButton at the top of the screen. The content frame will navigate to the Page1.xaml content, as shown below,

3. How to Navigate from One Page to another Silverlight Page. To accomplish this we will use NavigationService Object, an object that allows a view to access its hosting frame. Step 1: Lets Open the XAML forpage.xaml and modify the source to include a button under the TextBlock, as below <Grid x:name="layoutroot"> <StackPanel> <TextBlock Text="Page 1" FontSize="60" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" /> <Button Click="Button_Click" Padding="10" Content="Navigate to Inner Page" HorizontalAlignment="Center" /> </StackPanel> </Grid> Step 2: You we need to add the new Page that we will navigate to use the NavigationService. Right click on the Silverlight project and choose Add New Item. Select Silverlight Page as the template and name the file InnerPage1.xaml.

Step3: In the XAML for InnerView1.xaml, add a simple TextBlock. <Grid x:name="layoutroot"> <TextBlock Text="Inner Page 1" FontSize="40" Foreground="Blue" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> Step 4: Next, add the Button_Click event handler in the Page1.xaml code behind and add the following code private void Button_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/InnerPage.xaml", UriKind.Relative)); } Step 5: We are now ready to run the solution. Select Debug Start Debugging or press F5 to run the application. When Internet Explorer opens the application, click on the View 1 link at the top.

4. How to Pass Data between Navigation Pages. In this section, we will discuss passing data to page views within a navigation framework solution. In HTML pages, data is passed to other pages using the QueryString. The same is true for pages within a Silverlight navigation application through the use of the NavigationContext object. As an example, if we want to retrieve the QueryString property ProductID, we would use the following syntax: string productid = NavigationContext.QueryString["ProductID"].ToString(); Let s explore how to use the NavigationContext object to pass data to views. Step 1: Open the XAML for Page1.xaml and modify the source to include a ComboBox under the Button. <ComboBox Padding="10" Margin="10" x:name="color" Width="100"> <ComboBoxItem Content="Blue" IsSelected="True" /> <ComboBoxItem Content="Red" /> <ComboBoxItem Content="Green" /> </ComboBox> Step 2: Next open the code behind for Page.xaml and edit the Button_Click event handler to pass the selected color in the query string of the Uri passed to the Navigate method. private void Button_Click(object sender, RoutedEventArgs e) { string scolor = Color.SelectionBoxItem.ToString();

NavigationService.Navigate(new Uri(String.Format("/InnerPage.xaml?Color={0}", scolor), UriKind.Relative)); } Step3: Open the InnerPage1.xaml file and add a second TextBlock below the existing TextBlock using a StackPanel. <StackPanel> <TextBlock Text="Inner Page 1" x:name="viewheader" FontSize="40" Foreground="Blue" HorizontalAlignment="Center" VerticalAlignment="Center" /> <TextBlock Text="(Blue)" x:name="viewcolor" FontSize="30" Foreground="Blue" HorizontalAlignment="Center" VerticalAlignment="Center" /> </StackPanel> Step 4: Open the code behind for InnerView1.xaml and retrieve the passed color using the NavigationContext object. Then add a switch statement to change the color of the TextBlocks and edit the Text for the second TextBlock. protected override void OnNavigatedTo(NavigationEventArgs e) { string color = NavigationContext.QueryString["Color"].ToString(); Brush b; switch (color) { case "Red": b = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); ViewHeader.Foreground = b; ViewColor.Foreground = b; ViewColor.Text = "(Red)"; break; case "Green": b = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)); ViewHeader.Foreground = b; ViewColor.Foreground = b; ViewColor.Text = "(Green)"; break; default: b = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255)); ViewHeader.Foreground = b; ViewColor.Foreground = b; ViewColor.Text = "(Blue)"; break; } }

Step 5: We are now ready to run the solution. Select Debug Start Debugging or press F5 to run the application. When Internet Explorer opens the application, click on the Page 1 link at the top. The application should appear, as shown below

5. How to Hide the URL details, i.e. Uri Mapping In the preceding examples, we noticed the URL changing as you navigated to different Views in a frame. We also noticed that the URLs were not very pretty and contained some Information that we may not want to display. As an example, consider the following URL: http://www.domain.com/catalog.aspx#productdetails.xaml?id=4 For starters, this URL is not very pleasant to look at, and not very user-friendly either. It also may contain information that we would prefer not to provide the user, such as the exact filename and the query string name. A much more appropriate URL would look like the following: http://www.domain.com/catalog.aspx#product/4. So let s try out how to use URIMapping with Navigation Framework. Step 1: There are three views in our solution that you would like to add Uri Mapping for: Page1.xaml, Page2.xaml, and InnerPage.xaml. For these, we will add simple Uri Maps that point these to Page1, Page2, and InnerView. Let s start by opening the App.xaml file and adding the xml namespace for the navigation framework. <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="navigationfrmk.app" xmlns:navigation="clrnamespace:system.windows.navigation;assembly=system.windows.controls.navigation">

</Application> Step 2: Now that the namespace is added, we need to add the UriMapper section to the Application Resources. <Application.Resources> <navigation:urimapper x:key="urimapper"> </navigation:urimapper> </Application.Resources> Step 3: Within the UriMapper section we now need to add two UriMapping elements, one for Page1.xaml and one for Page2.xaml. Each mapping will contain two attributes: The Uri attribute is the name representing the mapping that will appear in the browser address bar, and the MappedUri attribute represents the actual Uri mapped to by the UriMapping. <Application.Resources> <navigation:urimapper x:key="urimapper"> <navigation:urimapping Uri="/Page1" MappedUri="/Page1.xaml"></navigation:UriMapping> <navigation:urimapping Uri="/Page2" MappedUri="/Page2.xaml"></navigation:UriMapping> </navigation:urimapper> </Application.Resources> Step 4: You we can update MainPage.xaml to navigate to the views using the UriMappings. <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <HyperlinkButton Content="View 1" Click="HyperlinkButton_Click" Padding="5" Tag="/Page1" > </HyperlinkButton> <HyperlinkButton Content="View 2" Click="HyperlinkButton_Click" Padding="5" Tag="/Page2"> </HyperlinkButton> </StackPanel> You notice here, we just changed Tag attribute from page1.xaml to page.xaml respectively. Step 5: Next, we will shift your attention to the InnerPage.xaml. If you recall in the previous section on passing data to a navigation view, we were passing the color to InnerPage.xaml via the QuervString. Because of this, we need that to be taken into account in your UriMapping. Open up the code behind for Page1.xaml and modify the Button_Click method so it navigates to InnerView/{0}. private void Button_Click(object sender, RoutedEventArgs e) { string scolor = Color.SelectionBoxItem.ToString();

//Old URL //NavigationService.Navigate(new Uri(String.Format("/InnerPage.xaml?Color={0}", scolor), UriKind.Relative)); //New URL NavigationService.Navigate(new Uri(String.Format("/InnerPage/{0}", scolor), UriKind.Relative)); } Step 6: In order for this navigates to work, we need to add an additional UriMapping to the Application.Resources. <Application.Resources> <navigation:urimapper x:key="urimapper"> <navigation:urimapping Uri="/Page1" MappedUri="/Page1.xaml"></navigation:UriMapping> <navigation:urimapping Uri="/Page2" MappedUri="/Page2.xaml"></navigation:UriMapping> <navigation:urimapping Uri="/InnerPage/{c}" MappedUri="/InnerPage.xaml?Color={c}"></navigation:UriMapping> </navigation:urimapper> </Application.Resources> Steo 7: Next, in the MainPage.xaml, add the UriMapper property to the Navigation Frame object. <navigation:frame x:name="contentframe" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="10" Grid.Row="1" BorderThickness="2" BorderBrush="Black" UriMapper="{StaticResource urimapper}"/> Step 8: We are now ready to run the solution. Select Debug Start Debugging or press F5 to run the application. When Internet Explorer opens the application, click on the View 1 link at the top. Notice that the URL now reads: http://localhost:3676/navigationfrmktestpage.aspx#/page1 http://localhost:3676/navigationfrmktestpage.aspx#/innerpage/blue As you have seen in this example, UriMapping provide a way to create more user friendly Url addresses and also provide a way to hide application specific information from appearing in your application. 6. Auto Integration with Browser History: The navigation features of the Frame control also integrate with the browser. Each time you call the Navigate() method, Silverlight adds a new entry in the history list. The first page of your application appears in the history list first, with the title of the HTML entry page. Each subsequent page appears under that in the history list, using the user-control file name for the display text (such as Page1.xaml).

7. Conclusion: here in this article we learned how to use Navigation Framework from scratch. We learned featured like, Page and Frame, Passing values between pages URI Mapping. Hope it did some needful. Read previous article on Navigation with User Control here : http://www.c- sharpcorner.com/uploadfile/37db1d/4958/default.aspx?articleid=cb0b291c-52ae-4b80-a95c- 438d76fa1145 Cheers!