WPF navigation-based Windows Applications

Source: Internet
Author: User
Use navigation in WPF. The content is organized in the page element, and the page can be hosted in the navigationwindow or frame.

These containers provide a page-to-page navigation, a log that records all navigation, and a series of navigation-related events.

<Navigationwindow xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" X: class = "wpf_test.mainwindow" Title = "form" Source = "page1.xaml"> </navigationwindow>

 

Navigation between pages:

1. Call the navigate Method

// Page1.xaml navigate to page2.xamlpage2 P2 = new page2 (); navigationservice. navigate (P2); // navigationservice. navigate (New uri ("page2.xaml", urikind. Relative ));

2. Use hyperlinks (inside the textblock tag)

<Textblock> 

 

3. Use navigation logs

Each navigation container contains logs that record the navigation history. The navigation log provides the logic of backward and forward.

Navigationwindow always has a navigation log, which may not exist in the frame, but can be set through the journalownership attribute. You can set false through navigationuivisibility to hide the navigation button.

// If you can retreat if (navigationservice. cangoforward) {navigationservice. goforward ();} else {navigationservice. Goback ();}

 

Data transmission between pages:

1. send data to the page

// Define a parameter namevaluecollection NVC = new namevaluecollection (); NVC ["num"] = "Lulu"; NVC ["num2"] = "66 "; // instantiate the page page2 P2 = new page2 (); // subscribe to the navigation loadcompleted event and add your own processing method to access the navigationservice parameter. loadcompleted + = p2.onloadcompleted; // page1.xaml navigate to page2.xaml and pass an NVC parameter navigationservice. navigate (P2, NVC );

// Defined in page2.xaml. CS to obtain the input parameter public void onloadcompleted (Object sender, navigationeventargs e) {If (E. extradata! = NULL) {namevaluecollection NVC = E. extradata as namevaluecollection; // use the obtained parameter to reset the label value label1.content = NVC [0];}

2. Add a parameter constructor to the page to transfer values between pages.

// Instantiate the page to navigate to page2 P2 = new page2 ("parameters from page1.xaml"); // navigate page1.xaml to page2.xamlnavigationservice. navigate (P2 );
// Define a parameter-included constructor that can be used to receive information transmitted on other pages. Public page2 (string value): this () {label1.content = value}

3. The properties set of the Application Object shares data globally.

Application.Current.Properties["globe"] = "globe";

Pagefunction returns data from the page:

Processing allows the user to navigate to a page and perform some operations. After the operation is completed, the user automatically returns to the previous page to avoid adverse consequences in navigation logs.

Pagefunction1 PGF = new pagefunction1 (); // process the return event PGF of pagefunction. return + = onreturn; // page1.xaml navigate to page2.xamlnavigationservice. navigate (PGF); // process pagefunction1 page return value private void onreturn (Object sender, returneventargs <string> E) {// reset the button2 content value with the returned result button2.content = E. result ;}
/* Pagefunction
 
  
Is a generic type. String is the returned data type */public partial class pagefunction1: pagefunction <string> {public pagefunction1 () {initializecomponent ();} private void button1_click (Object sender, routedeventargs e) {// indicates that the processing is complete and the current textbox1 value is returned to this. onreturn (New returneventargs <string> (this. textbox1.text ));}}
 

Custom dialog box:

<Window X: class = "wpf_test.mydialog" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: X = "http://schemas.microsoft.com/winfx/2006/xaml" Title = "mydialog" Height = "148" width = "280"> <grid> <label content = "Your Name: "Height =" 28 "horizontalalignment =" Left "margin =" 21,9, 108 "name =" label1 "verticalignment =" TOP "width =" "/> <textbox Height =" 23 "margin =, 27,0 "name =" textbox1 "verticalignment =" TOP "/> <button content =" Confirm "Height =" 23 "horizontalalignment =" Left "margin =" 37,70, 0, 0 "name =" button1 "verticalignment =" TOP "width =" 75 "Click =" button#click "/> <button content =" Remove "Height =" 23 "horizontalalignment =" right "margin =" 0, 70, 41,0 "name =" button2 "verticalignment =" TOP "width =" 75 "Click =" button2_click "/> </GRID> </WINDOW>
Private void button#click (Object sender, routedeventargs e) {// The form is displayed successfully. This is automatically disabled. dialogresult = true;} private void button2_click (Object sender, routedeventargs e) {// This. dialogresult = false ;}

Call custom dialog box

Mydialog MD = new mydialog (); Md. title = "Custom dialog box"; Md. windowstartuplocation = windowstartuplocation. centerowner; // the display window is blocked in the dialog box mode. showdilif returns a boolif (MD. showdialog () = true) {// (button) sender ). foreground = brushes. coral ;}

 

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.