The previous blog briefly introduced how to achieve page Jump in WP. Obviously, it does not make any sense to achieve page Jump. Here we will introduce the value transfer in WP pages.
There are roughly three methods for transferring values between WP pages: querystring, global variable, and independent storage. The lifetime of querystring and global variables is the current process, but the lifetime of independent storage is not run by the program.
. Because independent storage is a relatively separate and commonly used technology, I will write a blog post to introduce it separately.
First: querystring
Querstring method, that is, using a String constant to transfer the value between pages.
Example: This. navigationservice. navigate (New uri ("/page1.xaml? Value = ASD ", urikind. Relative ));
First, perform the page layout:
Process the button click event in mainpage. XAML:
Private void mb_click_1 (Object sender, routedeventargs E)
{
String uri = "/page1.xaml ";
Uri + = string. Format ("? Input1 = {0} ", MT. Text );
This. navigationservice. navigate (New uri (Uri, urikind. Relative ));
}
When the program is running, the URI is like this: "/page1.xaml? Input = ......" If you want to pass multiple parameters, you can use & to merge them, for example, "/page1.xaml? Input1 = ...... & Input2 = ......"
In page1.xaml. CS, rewrite the onnavigatedto function to assign values to the received value and the textbox attribute text:
Protected override void onnavigatedto (navigationeventargs E)
{
String value = string. empty;
Idictionary <string, string> querystring = This. navigationcontext. querystring;
If (querystring. containskey ("input1 "))
{
PT. Text = querystring ["input1"];
}
Base. onnavigatedto (E );
}
Effect:
The querystring method is applicable to simple and unidirectional page data transfer, because the querystring method cannot return data.
To solve the problem of returning data, it is best to learn how to use global variables.
Second, global variables:
For global variables, we can add a class that can be accessed throughout the life cycle of the program in the project and save the corresponding data in the attributes of the class. But now we don't have to do this extra work on our own. The project itself has a class to meet this requirement: app class. The app class inherits from the application class. The current attribute of the application can be used to obtain the application class instance associated with the current program.
After conversion, you can get the app class instance. All pages in the program can access this app class, so it is best to put the data we want to save into the app class, eliminating the trouble of self-implementation. Add a public inputstring attribute to the app class:
To use global variables, add the following code:
App = application. Current as app;
Process the Click Event of the button on the mainpage. XAML page
Private void mb_click_1 (Object sender, routedeventargs E)
{
App = application. Current as app;
App. inputstring = Mt. text;
This. navigationservice. navigate (New uri ("/pgae1.xaml", urikind. Relative ));
}
In page1.xaml. CS, rewrite the onnavigatedto function to assign values to the received value and the textbox attribute text:
Protected override void onnavigatedto (navigationeventargs E)
{
App = application. Current as app;
PT. Text = app. inputstring;
Base. onnavigatedto (E );
}
The effect is as follows:
The preceding querystring and global variables can be used for page value transfer.
Here, I will talk about my experience in mobile development. I didn't know much about it at the very beginning. I really admire it when I saw many "Great gods" Who could use many methods to implement a function.
Every time I encounter problems, I try to implement them in different ways. However, as the development time grows, I find that this is a very bad habit. In fact, for different
There should be different solutions to the problem, and different solutions have different advantages and disadvantages. During development, you should try to use a skillful and efficient way to solve the problem, instead of blindly solving the problem
The number of solutions is the target.
Jiang Zi