Sliverlight page jumps and transfers values recently learned !!

Source: Internet
Author: User

References: http://www.silverlightchina.net/html/tips/2010/0119/600.html

1. Jump

I use the Root View to redirect. In fact, this step is the jump between the login and the content homepage, during which no value is transferred. Because I think the user name is still defined in APP. XAML, and it is better to set it to global. The following is the implementation, so I will not detail the direct reference:

●Use the Root View (Root
Visual)
The above is a simple example. This method is very common, but not a general method. Its small defect is that the layout of the entire page is fixed, and the toolbar or page is always fixed there. If you want a brand new page, it won't work. Let's take a look at this small example to extend it to achieve our goal.
First, declare a grid in APP. XAML. CS: 

Private Grid Rootgrid
= New Grid();
Then modify the appliaction_startup event.Code:

This. Rootvisual = rootgrid; 

Rootgrid. Children. Add (New Page1());

In this way, you can only ensure that there is a page during initialization and you cannot navigate. Therefore, add a static method to the app. XAML. CS Code. The Code is as follows: 
Public Static Void Navigation (Usercontrol Newpage) 

 // Obtain the current Appliaction instance 
 App Currentapp = (App)Application. Current; 
 // Modify the content of the currently displayed page. 

Currentapp. rootgrid. Children. Clear (); 

Currentapp. rootgrid. Children. Add (Newpage ); 
}
Note that
The method parameter is usercontrol. In this way, you can add the following code (which can be added to the button event) in your usercontrol to switch the page: 

App. Navigation (New Page2());
The new object is your target page. Do not make any mistakes. These several codes have completed our goal!

●Save Page Status (cache)
If you want the user to return to the history page, you can modify the status of the page, such as the data entered by the user. First, make a small modification to the project, and add an enmu named pages to save the Page name so that you can use strings to generate unnecessary problems: 
Public Enum Pages 

Page1, 
Page2 
}
The next step is to add a generic set to the app. CS code to save the page: 

Private   Static   Dictionary < Pages ,   Usercontrol > Pagecache =   New   Dictionary < Pages ,   Usercontrol > ();
The key is the pages enumeration, and the value is usercontrol. Then define the naviagte method again: 
Public Static Void Navigation (Pages Newpage) 

 App Currentapp = (App)Application. Current; 
 If (! Pagecache. containskey (Newpage )) 

// Use reflection based on the name to create the target page instance and add it to the cache 
Type Type =
Currentapp. GetType (); 
Assembly Assembly
= Type. assembly; 
Pagecache [Newpage] =
(Usercontrol) Assembly. createinstance ( 
Type. namespace + "." + Newpage. tostring ()); 


Currentapp. rootgrid. Children. Clear (); 

Currentapp. rootgrid. Children. Add (pagecache [Newpage]); 
}
In this way, the following code can be called in other usercontrol to switch: 

App. Navigation (Pages. Page2 );

After logging on to the home page, we will jump between tab labels, add a tabcontrol on the page, and add a button, click the button to dynamically Add a tab (jump to another XAML page)

  Private   Void Button#click ( Object  Sender, routedeventargs e) {tabitem temptb = New  Tabitem (); tab1 UC = New Tab1 (textbox1.text ); //  The XAML page to jump to, tab1 Temptb. header ="  Tab  "  ; Temptb. Width = 100  ; Temptb. Content = UC;  This  . Tabcontrol1.items. Add (temptb); tabcontrol1.selectedindex = Tabcontrol1.items. Count- 1  ;} 

The page Jump is generally like this, which is more general. The rest is to pass the value.

2. Pass the value

1. Transfer value between the login page and the Home Page:

Declare a global variable in APP. XAML. CS and store the login username. Assign a value to this variable in the logon button event.

 
APP pub = application. CurrentAsAPP;StringName = pub. Name;//Can be used directly

 

You can directly use this variable on another page.

 
APP pub = application. CurrentAsAPP;StringName = pub. Name;

 

2. Value Transfer Between tab pages.

Http://silverlightchina.net/html/tips/2012/0517/16024.html

In mainpage2.xaml. CS, we first define the attribute of the parameter or the constructor parameter, for example:

Public long ID {Get; set ;}

Public String PWD {Get; set ;}

Or:

Public mainpage2 (long ID, string PWD ){

Initializecomponent ();

}

To switch from mainpage1 to mainpage2, remove the mainpage1 instance in canvas or grid:

Layoutroot. Children. Remove (mainpage1 );

Then, the following parameters are carried to create the mainpage2 instance:

Mainpage2 mainpage2 = new mainpage2 (){

Id = p1,

Pwd = P2,

...

};

Or:

Mainpage2 mainpage2 = new mainpage2 (P1, P2 ,...);

Add mainpage2 to layoutroot:

Layoutroot. Children. Add (mainpage2 );

In fact, the public attribute or constructor is used to pass the value. When we jump between tabs, we can directly pass the value when we click the event instantiation page.

 

The logic may be messy due to the first technical posting. Thank you for your attention ..

 

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.