WPF learning: navigation

Source: Internet
Author: User

========================================================== ========================

This article is reprinted. You must ensure that this article is complete and the original author information and the link to this Article are fully retained.

E-mail:Khler@163.com

QQ: 23381103

MSN:Pragmac@hotmail.com

Http://www.rainsts.net/article.asp? Id = 737

========================================================== ========================

 

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 of 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 a control environment similar to 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 also supports navigation and positioning like "test.htm # name" to scroll the page until the control with a specific name is displayed. Another utility property 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

In most cases, we need to use navigationservice instead of hyperlink. navigateuri, such as non-default constructed pages and dynamic target pages. You can use page. navigationservice or navigationservice. getnavigationservice () to obtain 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

The use of page itself is not the content of this article. Here we are concerned about its lifecycle in the navigation process. In WPF, page is destined to be a zombie. 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, logs only record the status data of page-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>

Note that this method is only valid for log operations such as forward and backward. If we use the hyperlink. navigateuri or navigationservice. navigate () navigation, a new page instance will still be generated and may replace the last object reference record of the same type in the log. In addition, when multiple pages have cyclic links, multiple page instances will be logged, resulting in a certain amount of 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 functions similar to HTML showmodal. 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-like method. 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. Is it just because of the special page instance construction logic? We can also use application. properties + page to implement a non-correlated and coupled showmodel logic, but it is 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.

 

 

 

Related Article

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.