The navigation framework in WPF

Source: Internet
Author: User

Sometimes, we need a UI that supports page jumps, such as a file browser, starting a wizard, and so on. For such interface, simple can use ContentControl + contenttemplateselector way to achieve, but sometimes we will need some more advanced jump function, such as forward, fallback and so on. At this point, this is a little bit of a struggle, and we can use the WPF navigation framework to help us quickly implement this feature.

The page framework for WPF consists mainly of two parts, containers and pages,

Here is a simple example of WPF's page framework, first we create the first page:

<page x:class="Wpfapplication1.page1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"XMLNS:MC="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"mc:ignorable="D"D:designheight=" -"D:designwidth=" -"Title="Page1"> <Grid> <TextBlock> <run>### this isPage1, let's Go to</run>"Page2.xaml">Page2</Hyperlink> </TextBlock> </Grid></Page>

And then create a second page,

<page x:class="Wpfapplication1.page2"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"XMLNS:MC="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"mc:ignorable="D"D:designheight=" -"D:designwidth=" -"Title="Page2"> <Grid> <TextBlock> <run>~~~ this isPage2, let's Go to</run>"Browseback">Page1</Hyperlink> </TextBlock> </Grid></Page>

Finally we host them in the container, in WPF, the page's container can be Window,NavigationWindow,frame , or browser, and most of the time with frame and NavigationWindow, because it provides a series of navigation-related functions in which frame is more flexible, here's a frame example to describe its usage:

<window x:class="Wpfapplication1.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow"height=" -"Width="525"> <Grid> <frame x:name="Frame"Source="Page1.xaml"navigationuivisibility="Visible"> </Frame> </Grid></Window>

After you run the above code, you get a navigation window that jumps between the two pages below. Click Page1 Link can jump to Page2, click Page2 Link can fall back to Page1

page Address :

In the WPF Navigation framework, page addresses are represented by URIs and do not require the creation of a Page object manually (which can also be created manually), such as the source= "Page1.xaml" set in the frame. It sets the URI of the start page to Page.xaml, and the Page1 object is created automatically.

Page Jump:

Page jump is controlled by NavigationService , in the frame and page has the name NavigationService object, you can use its navigate function to implement the page jump. For example, the previous setting of Source= "Page1.xaml" in frame is actually a jump implemented by the following function:

Frame. Navigationservice.navigate (new Uri ("page1.xaml", urikind.relative));

This function is not limited to the URI, the jump object is not limited to the URI, the following way is also possible.

Frame. Navigationservice.navigate (new  Page1 ()); frame. Navigationservice.navigate (new  Button ()); Frame. Navigationservice.navigate ("HelloWorld");

In addition, we can implement page jumps in the XAML of page with the NavigateUri property of hyperlink like Page1.xaml, of course, it is also called navigationservice.navigate to implement.

Navigation commands:

In addition to the page jumps, NavigationService also provides some basic navigation commands, such as forward, fallback, refresh. can be done by

Frame. Navigationservice.goforward (); frame. Navigationservice.goback (); frame. Navigationservice.refresh ();

In addition, WPF itself provides a set of standard navigation commands navigationcommands(than NavigationService), Page and frame also support the binding of these commands (Navigationcommands commands are more than NavigationService can support), so we can use command-line binding to invoke these functions very conveniently. For example, the fallback command used by the Page2 species:

"browseback" >Page1</Hyperlink>

Finally, a simple introduction of a few technical content, but very common features, that is, the frame object's navigation toolbar redraw. The frame object itself is accompanied by a navigation bar, which provides a forward and backward function similar to IE. Visible when the navigationuivisibility is set to visible or auto.

But this toolbar is too primitive, debugging can also, in the final delivery of either hide it, or rewrite it, the way to rewrite the general is to rewrite its template, as follows is a simple example:

<controltemplate targettype="Frame"> <dockpanel margin="8"> <stackpanel margin="4"dockpanel.dock="Top"orientation="Horizontal"> <button content="Go Back"margin="4"Command="{x:static Navigationcommands.browseback}"/> <button content="Go Forward"margin="4"Command="{x:static Navigationcommands.browseforward}"/> </StackPanel> <border borderbrush="Orange"margin="7"borderthickness="4"padding="7"Cornerradius="7"Background=" White"> <contentpresenter/> </Border> </DockPanel></ControlTemplate>

But this is often used for subsequent reference.

Http://www.cnblogs.com/tmywu/archive/2010/05/25/1743689.html

Summary:

This article mainly introduces the basic usage of the navigation framework of WPF, and then writes the article after more functions. Or see Microsoft's official MSDN: Navigation Overview

The navigation framework in WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.