WPF Study Notes-3. Navigation

Source: Internet
Author: User

The rise of the Internet has created and cultivated a new user interaction interface-page &
Navigation. Both forward, backward, and page are completely new categories, different from those of SDI/MDI in the past. WPF or its simplified version
Silverlight inevitably follows this improved B/S mode and uses URI to concatenate the UI process.

Navigationservice,
Page, hyperlink, and Journal are the core of the overall WPF navigation system. Navigationservice provides
In the control environment of IE host, Journal can record and restore the status of related pages. We usually choose the host Methods: browser (xbap) and
Navigationwindow.

1. navigationwindow

Navigationwindow is inherited from window. For some reason, I did not find related entries in vs2008 "new item...". I had to change a window to navigationwindow by myself.

Window1.xaml

<Navigationwindow X: class = "window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "300" width = "300" windowstartuplocation = "centerscreen"
Source = "page1.xaml">
</Navigationwindow>

The source attribute specifies the default page of the window. Of course, we need to modify the base class in window1.xaml. CS.

Public partial class window1: navigationwindow
{
Public window1 ()
{
Initializecomponent ();
}
}

To create a page1.xaml, we can add related controls and operations like normal windows.

2. hyperlink

Hyperlinks should be the most familiar navigation method.

Page1.xaml

<Page X: class = "learn. WPF. page1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "page1">
<Grid>
<Textblock>
<Hyperlink navigateuri = "page2.xaml"> page2 </Textblock>
</GRID>
</Page>

Navigateuri is equivalent to "html a. href". Of course, you can also use the hyperlink. Click Event and then use navigationservice to complete the navigation operation.

Page1.xaml

<Hyperlink click = "hyperlink_click"> page2 Page1.xaml. CS

Private void hyperlink_click (Object sender, routedeventargs E)
{
This. navigationservice. navigate (New uri ("page2.xaml", urikind. Relative ));
}

Hyperlink
It also supports navigation and positioning like "test.htm # name", scrolling the page until the control with a specific name is displayed. Another utility of Hyperlink is
Command, we can use a series of static members created in navigationcommands to perform some common operations.

<Hyperlink command = "navigationcommands. Refresh"> refresh <Hyperlink command = "navigationcommands. browseback"> browseback <Hyperlink command = "navigationcommands. browseforward"> browseforward 3. navigationservice

Very
In most cases, we need to use navigationservice instead of hyperlink. navigateuri.
Page to dynamically determine the target page. We can use page. navigationservice or
Navigationservice. getnavigationservice () obtains the instance reference of navigationservice.
(Do not forget to add using system. Windows. Navigation ).

Public partial class page1: Page
{
Private void hyperlink_click (Object sender, routedeventargs E)
{
VaR page2 = new page2 ();
Page2.label1. content = "Beijing 2008! ";

This. navigationservice. navigate (page2 );
}
}

In addition to navigate (), you can also use the two attributes of navigationservice to complete the navigation switch operation.

// This. navigationservice. content = page2;
This. navigationservice. Source = new uri ("page2.xaml", urikind. Relative );

Navigationservice provides a lot of methods and time to manage related navigation operations.

Logs: addbackentry and removebackentry.
Load: navigate, refresh, and stoploading.
Switch: Goback and goforward.
Event: Navigating (triggered when a new navigation request is sent, you can cancel the navigation )......

We can also use application-related events to process the navigation process.

4. Journal

Journal is equivalent to webbrowser. history. It contains two data stacks to record the display status of the forward and backward pages. Each related page corresponds to a journalentry. Automatic Log status recovery is only valid when you click the forward and backward buttons on the navigation bar.

5. Page

Yes
The use of the reference page is not the content of this article. Here we are concerned about its lifecycle in the navigation process. In WPF, page
It is doomed to be a short-lived ghost. No matter whether we use the navigation or back button, we will re-create the page object instance, and then it may be the log to restore its display status. That is to say, the log only records the page
Status data of related controls, rather than Page Object Reference (by default ).

There are two ways to maintain a page reference. The first is to maintain a page reference, such as using a container like application. properties.

Private void hyperlink_click (Object sender, routedeventargs E)
{
VaR page2 = application. Current. properties ["page2"] As page2;
If (page2 = NULL)
{
Page2 = new page2 ();
Page2.label1. content = datetime. Now. tostring ();

Application. Current. properties ["page2"] = page2;
}

This. navigationservice. navigate (page2 );
// This. navigationservice. content = page2;
}

The other is to set the page. keepalive attribute, so that the log will record the reference of the page. When we use the forward and backward buttons, the object instance of the page will not be created again.

<Page X: class = "learn. WPF. page2"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "page2" loaded = "page_loaded"
Keepalive = "true">

</Page>

Yes
Note: This method is only valid for log operations such as forward and backward. If we use hyperlink. navigateuri or
Navigationservice. navigate ()
A new page instance will still be generated during navigation, and may replace the last reference record of the same type in the log. In addition, when multiple pages have a circular link, multiple page instances will be logged, resulting in
Fixed memory waste.

6. Frame

The role of frame is similar to the IFRAME in HTML. We can use it to nest other pages in a common window or page.

<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "300" width = "300">
<Grid>
<Frame source = "page1.xaml"> </frame>
</GRID>
</WINDOW>

By default, frame tries to use the logs of the upper page or form (navigationwindow). Of course, we can also use the journalownership attribute to force the frame to use its own log navigation.

<Frame source = "page1.xaml" journalownership = "ownsjournal"> </frame>

Another function of frame is to navigate to the HTML page and use it as an embedded ie webbrowser.

<Frame source = "http://www.rainsts.net"/>

7. pagefunction <t>

WPF
Provides a page inheritance class called pagefunction to implement HTML showmodal-like
. We can use it to collect some data and return it to the call page. Of course, this encapsulation is actually very simple and we can implement it by ourselves. It is nothing more than providing an onreturn
. The generic parameter t indicates the data type returned.

Page1.xaml. CS

Public partial class page1: Page
{
Private void hyperlink_click (Object sender, routedeventargs E)
{
VaR modal = new pagefunction1 ();
Modal. Return + = (S, ex) => This. label1.content = ex. Result. tostring ();
This. navigationservice. navigate (Modal );
}
}

Pagefunction1.xaml. CS

Public partial class pagefunction1: pagefunction <int>
{
Private void button#click (Object sender, routedeventargs E)
{
Onreturn (New returneventargs <int> (datetime. Now. millisecond ));
}
}

Procedure:
(1) create a pagefunction <t> object instance. Of course, we can use the construction with parameters to transmit additional data;
(2) Call the pagefunction <t>. onreturn () method to return a specific result packaging object -- returneventargs <t>;
(3) The caller obtains the returned result by subscribing to the pagefunction <t>. Return event.

It is also mentioned in msdn that onreturn (null) is used to represent cancel,
~~~~ To be honest, I personally think this pagefunction is a bit awkward from naming to execution logic, just because page
What is the special instance construction logic? We can also use application. properties + page to implement a non-correlated and coupled showmodel.
Logic, but not so "standard.

One thing to note: we should promptly release the subscription to functionpage <t>. return. The above example is just as lazy as msdn.

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.