Windows phone Data Sharing methods

Source: Internet
Author: User
Tags comments touch xmlns

This section describes how to implement data sharing. First, two pages are created. When the MainPage is navigated to the SecondPage through the event, we need to pass some content in the MainPage (such as a string) to the SecondPage, and the SecondPage page shows the passed Content. When the page SecondPage is navigated to the MainPage through the event, we also pass some content (such as a string) to the MainPage;

In the created MainPage. xaml file, I added only one Button element, set the displayed Content, and defined the touch events of the element:
<Button x: Name = "btn" Content = "navigate to the second page" Grid. Row = "1" Click = "btn_Click"> </Button>
The following namespace must be referenced in the hidden file of MainPage:
// Reference the namespace -- used by the PhoneApplicationService class
The code is as follows: Copy code
Using Microsoft. Phone. Shell;
The code for the hidden MainPage file is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using Microsoft. Phone. Controls;
// Reference the namespace -- used by the PhoneApplicationService class
Using Microsoft. Phone. Shell;

Namespace Upload data
{
Public partial class MainPage: PhoneApplicationPage
    {
// Constructor
Public MainPage ()
        {
InitializeComponent ();
        }
/// <Summary>
/// Click to navigate to the second page
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btn_Click (object sender, RoutedEventArgs e)
        {
This. NavigationService. Navigate (new Uri ("/SecondPage. xaml", UriKind. Relative ));
        }
// Knowledge point ①
Protected override void OnNavigatedFrom (System. Windows. Navigation. NavigationEventArgs e)
        {
// Target page -- knowledge point ②
If (e. Content is SecondPage)
            {
(SecondPage) e. Content). ApplicationTitle. Text = "data transferred successfully! ";
            }
// Obtain the reference of the application object-knowledge point ③
(Application. Current as App). shareData = "Sharing data through APP class attributes ";
// Application status Management --- Knowledge Point ④
PhoneApplicationService. Current. State ["Share"] = "temporary data ";

Base. OnNavigatedFrom (e );
        }

///// <Summary>
//// Accept the passed value
///// </Summary>
///// <Param name = "e"> </param>
// Protected override void OnNavigatedTo (System. Windows. Navigation. NavigationEventArgs e)
//{
/// Obtain the shared data in the App class
// PageTitle. Text = (Application. Current as App). Parse data. ToString ();
// If (PhoneApplicationService. Current. State. ContainsKey ("Share "))
//{
/// Obtain the state attribute set in the phoneapplicationService object
// PageTitle. Text + = "n" + PhoneApplicationService. Current. State ["Share"]. ToString ();
//}
// Base. OnNavigatedTo (e );
//}
    }
}
 
 
 
 
In the created SecondPage. xaml file, I added only one Button element, set the displayed Content, and defined the touch events of the element:
<Button x: Name = "btn" Content = "navigate to 1st pages" Grid. Row = "1" Click = "btn_Click"> </Button>
The hidden files of SecondPage also need to reference the same namespace:
// Reference the namespace -- used by the PhoneApplicationService class
Using Microsoft. Phone. Shell;
 
The code of the hidden SecondPage file is as follows:
 
The code is as follows: Copy code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using Microsoft. Phone. Controls;
// Reference the namespace -- used by the PhoneApplicationService class
Using Microsoft. Phone. Shell;

Namespace Upload data
{
Public partial class SecondPage: PhoneApplicationPage
    {
Public SecondPage ()
        {
InitializeComponent ();
        }
/// <Summary>
/// Accept the passed value
/// </Summary>
/// <Param name = "e"> </param>
Protected override void OnNavigatedTo (System. Windows. Navigation. NavigationEventArgs e)
        {
// Obtain shared data in the App class
PageTitle. Text = (Application. Current as App). Parse data. ToString ();
If (PhoneApplicationService. Current. State. ContainsKey ("Share "))
            {    
// Obtain the state attribute set in the phoneapplicationService object
PageTitle. Text + = "n" + PhoneApplicationService. Current. State ["Share"]. ToString ();
            }
Base. OnNavigatedTo (e );
        }
/// <Summary>
/// Navigate to the first page
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btn_Click (object sender, RoutedEventArgs e)
        {
This. NavigationService. GoBack ();;
        }


// Protected override void OnNavigatedFrom (System. Windows. Navigation. NavigationEventArgs e)
//{
// If (e. Content is SecondPage)
//{
// (SecondPage) e. Content). PageTitle. Text = "The data is successfully transferred! ";
//}
// (Application. Current as App). shareData = "Share data through APP class attributes ";

// PhoneApplicationService. Current. State ["Share"] = "temporary data ";

// Base. OnNavigatedFrom (e );
//}
  
 
One of the three parameters is to share multiple pages by setting attributes under the App class, directly setting the public attributes in the App class:
// Set shared data
Public string collect data {get; set ;}
So far, all the code has been presented here, so how do we share data? OK, details are as follows: first, click the button event in MainPage, because no other code needs to be executed, the OnNavigatedFrom method on the MainPage will be executed immediately after the component is executed. We can see that the OnNavigatedFrom parameter is System. windows. navigation. navigationEventArgs is the parameter for navigation. After the OnNavigatedFrom method is executed, it will navigate to the SecondPage page. The Hidden file of SecondPage loads the constructor first, and then the OnNavigatedTo method.
The OnNavigatedTo method accepts transmitted data. In the same principle, we can pass the data in SecondPage to MainPage. The commented out part of the code is to implement this content, mainPage hides the comments in the file. If you remove the comments, an error will be reported during running, because the OnNavigatedFrom method will be executed after the program enters the MainPage page and triggers the Button event, at this time, no content is transmitted;
 
 
Here, OnNavigatedFrom is rewritten to be last executed when the page becomes inactive, so some operations can be completed on this page; as we can assign values here to the Text attribute in the TextBlock element of the target page;
 
 
System. windows. navigation. the NavigationEventArgs parameter records some information when we click the navigation bar. e. content indicates the page we want to navigate to, such as (SecondPage) e. content ). applicationTitle. text = "data passed! "; We also access the attributes of the target.
 
 
The App class is used to share the parameter because all pages in the program can access the App class derived from the Application, and the attributes set in the App class can also be accessed, the static property of the Application class Current returns a reference to the Application object. If you want to convert it to a derived App class, you can forcibly convert it.
 
The PhoneApplicationService class is instantiated in the App. xaml file.
<Application
X: Class = "using data. App"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: phone = "clr-namespace: Microsoft. Phone. Controls; assembly = Microsoft. Phone"
Xmlns: shell = "clr-namespace: Microsoft. Phone. Shell; assembly = Microsoft. Phone">

<! -- Application resources -->
<Application. Resources>
</Application. Resources>

<Application. ApplicationLifetimeObjects>
<! -- Objects required to process the lifetime events of an application -->
<Shell: PhoneApplicationService
Launching = "Application_Launching" Closing = "Application_Closing"
Activated = "Application_Activated" Deactivated = "Application_Deactivated"/>
</Application. ApplicationLifetimeObjects>

</Application>
 
The PhoneApplicationService class provides temporary data storage, which can be retained only during running (Independent storage spaceCan be permanently retained). The State attribute of this class can be set as a dictionary container, but all objects stored in the State Dictionary must be serializable, serializable means that the object can be converted to XML, and XML can also be deserialized into an object.

 

 
Method for loading the last page: OnNavigatedFrom method; obtain the target page of the navigation: e. Content and perform Forced conversion; access the public attributes of the App class; and save and read PhoneApplicationService. State temporarily;
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.