original text :windows phone Three ways to share data (8)

The content of this section is data sharing , Description of the effect of the implementation : The first is to create two pages , When the page MainPage Navigate to the page through events SecondPage yes , We need to MainPage Some of them ( Like a string ) Pass on to SecondPage in ,SecondPage The page presents the delivered content , When the page SecondPage Navigate to the page through events MainPage When , We also put some content ( Like a string ) Delivery and page MainPage;

In the establishment of MainPage.xaml I only added one to the file Button Elements , Set the displayed Content Content , And the touch event of this element is defined :

<Button x:Name="btn" Content=" Navigate to the second page " Grid.Row="" Click="btn_Click"></Button>

MainPage First of all, you need to refer to the following namespace

// Reference namespace --PhoneApplicationService Class using Microsoft.Phone.Shell;

MainPage The full code of the hidden 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 namespace --PhoneApplicationService Class
using Microsoft.Phone.Shell;

namespace ShareData
{
    public partial class MainPage : PhoneApplicationPage
    {
        //  Constructors
        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 transfer successful !";
            }
            // get application References to objects -- Knowledge point ③
            (Application.Current as App).shareData = " adopt APP Class to share data ";
            // State management of applications --- Knowledge point ④
            PhoneApplicationService.Current.State["Share"] = " Temporary data ";

            base.OnNavigatedFrom(e);
        }

        ///// <summary>
        /////  Accept the value passed
        ///// </summary>
        ///// <param name="e"></param>
        //protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        //{
        //    // get App Shared data in class
        //    PageTitle.Text = (Application.Current as App).shareData.ToString();
        //    if (PhoneApplicationService.Current.State.ContainsKey("Share"))
        //    {
        //        // get phoneapplicationService Object state attribute
        //        PageTitle.Text += "\n" + PhoneApplicationService.Current.State["Share"].ToString();
        //    }
        //    base.OnNavigatedTo(e);
        //}
    }
}

In the establishment of SecondPage.xaml I only added one to the file Button Elements , Set the displayed Content Content , And the touch event of this element is defined :

<Button x:Name="btn" Content=" Navigate to page 1 A page " Grid.Row="" Click="btn_Click"></Button>

SecondPage The hidden files of need to refer to the same namespace :

SecondPage The full code of the hidden 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 namespace --PhoneApplicationService Class
using Microsoft.Phone.Shell;

namespace ShareData
{
    public partial class SecondPage : PhoneApplicationPage
    {
        public SecondPage()
        {
            InitializeComponent();
        }
        /// <summary>
        ///  Accept the value passed
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // get App Shared data in class
            PageTitle.Text = (Application.Current as App).shareData.ToString();
            if (PhoneApplicationService.Current.State.ContainsKey("Share"))
            {    
                // get phoneapplicationService Object state attribute
                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 = " Data transfer successful !";
        //    }
        //    (Application.Current as App).shareData = " adopt APP Class to share data ";

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

        //    base.OnNavigatedFrom(e);
        //}
  

One of our three ways to pass parameters is to use App Class to share multiple pages , Directly in App Class :

  // Set up shared data         public string shareData { get; set; }

So far, all the code has been presented here , How do we share data ,ok, Details are as follows : First we click MainPage Medium button event , Because there's nothing else to do , This will be executed immediately after execution MainPage On the page OnNavigatedFrom Method , We can see OnNavigatedFrom The parameter is System.Windows.Navigation.NavigationEventArgs It's the parameters of our navigation ,OnNavigatedFrom After the method is executed, you will navigate to SecondPage page ,SecondPage The first thing to load is the constructor , Then is OnNavigatedTo Method , So we are

OnNavigatedTo Method to accept the passed data ; By the same principle, we can put SecondPage The data in is transferred to MainPage in , The commented out part of the code is to implement the content , among MainPage Hide the commented out parts of the file , If the comments are removed , When running, it will report an error , Because before the program enters MainPage Page and trigger Button It will be executed after the event OnNavigatedFrom Method , And there's no content delivery at this point ;

Rewrite here OnNavigatedFrom The method is to execute when this page becomes inactive , So some operations can be done on this page ; As we have here, we can assign values to the target page TextBlock In the element Text attribute ;

System.Windows.Navigation.NavigationEventArgs Parameters record some information when we click navigation , among e.Content Represents the page we want to navigate to , like this ((SecondPage)e.Content).ApplicationTitle.Text = " Data transfer successful !"; We also access the properties of the target

adopt App Class to share parameters because all pages in the program can be accessed Application Derived App class , stay App Of course, we can also access the properties set in the , among Application Static properties of class Current The return is Application References to objects , If you want to convert to derived App class , Forced conversion is enough

PhoneApplicationService Class instantiation in App.xaml In file

<Application 
    x:Class="ShareData.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 needed to handle lifetime events for an application -->
        <shell:PhoneApplicationService 
            Launching="Application_Launching" Closing="Application_Closing" 
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>
PhoneApplicationService Class provides temporary storage of data , It can only be retained when it is running ( Independent storage space It can be kept forever ), Properties of this class State What can be set up is the dictionary container , But keep it in State All objects in the dictionary must be serializable , Serializable means that you can convert an object to XML,XML It can also be de sequenced into objects

The last way to load the page :OnNavigatedFrom Method ; Get the navigation target page :e.Content And cast ; visit App Class public properties ;PhoneApplicationService.State Save and read temporarily ;

Source download

windows phone Three ways to share data (8) More articles about

  1. mutex,thread(c++11 windows linux Three ways )

    One c++11  windows linux Three ways //#include <stdio.h> //#include <stdlib.h> //#include <uni ...

  2. VMware There are three different ways of network connection

    About VMware Three ways to connect to the Internet ,NAT,Bridged,Host-Only , In the first contact, we usually encounter the host Ping Not virtual machine, but virtual machine Ping Passable host : The host and virtual machine are not connected to each other and so on . This article is about these three ...

  3. [ turn ] detailed DHCP There are three types of servers IP Distribution method

    DHCP Dynamic host configuration protocol (Dynamic Host Configuration Protocol), Its purpose is to reduce TCP/IP Network planning . The burden of management and maintenance , solve IP Lack of address space . Such networks ...

  4. .NET There are three ways to implement interfaces in

    Excerpt from :http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html Generally speaking .NET Three different interface implementations are provided , They are implicitly connected ...

  5. Apache Spark explore a mystery : Comparison of three distributed deployment methods

    from : Link address : http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/     Objective ...

  6. Binding in Elementname,Source,RelativeSource Three ways to bind

    stay WPF In the process of application development Binding It's a very important part . In the actual development process Binding Different ways of writing achieve the same effect, but the fact is that there is a big difference . Here will be the actual problems encountered in the next summary record and understanding . 1. so ...

  7. Start at one and separate the front and back ends 【 .NET Core2.0 +Vue2.0 】 Frame 12 || Comparison of three cross domain approaches ,DTOs( Data transmission object ) On

    Update feedback 1. Bo you @ It's about the end of the curtain ,Nginx Reverse proxy implementation across domains , Because I haven't used it yet , It's ignored , This time it was recorded , For the next supplement . This pit has been filled 2. Tips : Cross domain sister ——< Thirty-three ║ ⅖ There are two ways to achieve the perfect cross ...

  8. Kubernetes There are three external access methods of :NodePort、LoadBalancer and Ingress( forward )

    original text http://cloud.51cto.com/art/201804/570386.htm Kubernetes There are three external access methods of :NodePort.LoadBalancer and Ingress lately ...

  9. Framework source code series 9 : Dependency injection DI、 Three Bean Configuration mode registration and instantiation process

    One . Dependency injection DI Learning goals 1) Make clear the process and class of constructing parameter dependency injection 2) Find out where annotation based attribute dependency injection is done . Learning ideas 1) Think about how we do it by hand 2) read spring Compare the source code to see its implementation 3)Spring ...

Random recommendation

  1. [ translate ]Godot Series four - Write a script

    Write a script (Scripting) brief introduction There's a lot of talk about tools that can create video games without programming . Not having to learn programming knowledge is a dream for many independent developers . This demand - Game developer . Even within many companies , I hope that the game process ...

  2. winfrom Limit text box to two decimal places

    private void numWeight_KeyPress(object sender, KeyPressEventArgs e) { if (char.IsNumber(e.KeyChar) | ...

  3. logstash Handle nginx Access log

    [root@dr-mysql01 frontend]# cat logstash_frontend.conf input { file { type => "zj_frontend_a ...

  4. begging N! How many in the world 0

    analysis : Yes N Prime factor decomposition N=2^x * 3^y * 5^z.... because 10 = 2*5, So at the end 0 The number of is only equal to x And z of , Each pair of 2 and 5 Multiply to get a 10. So at the end 0 The number of =min(x,z). In practice, ...

  5. Uniformity Hash The practical application of the algorithm

    Preface I remember sharing one a year ago < Uniformity Hash Algorithm analysis >, I just analyzed the implementation principle of this algorithm . What problems have been solved . But there is no actual implementation of such an algorithm , After all, to deepen the impression also have to rub their own , So this time we're going to talk about the current ...

  6. Reptile base ——HTTP The basic principle

    ## Learning crawler must start with understanding the workflow of request web page and the composition principle of web page , Otherwise, learn the crawler operation directly, such as request library and so on , Probably I will know it, but I don't know why ( Personal experience ) URL and HTTP brief introduction URL(Uniform Resour ...

  7. Hbase Record -HBase Additions and deletions

    HBase Create data   This chapter will show you how to HBase Data created in the table . To be in HBase Data created in the table , You can use the following commands and methods : put  command , add() - Put Class method put() - HTable class ...

  8. [ turn ]springcloud( Nine ): Configuration center and message bus ( Configuration center final )

    https://www.cnblogs.com/ityouknow/p/6931958.html springcloud( Nine ): Configuration center and message bus ( Configuration center final ) We are springcloud( 7、 ... and ) ...

  9. ruby Medium win32ole Use

    ruby Medium win32ole It's a standard library , When using, just add require 'win32ole' Just go . The following is a simulation of a login code require 'win32ole' ie = WIN32OLE. ...

  10. linux A brief analysis of the file system writing process

    linux The process of writing to disk goes through VFS ->   Page caching (page cache) -> Specific file systems (ext2/3/4.XFS.ReiserFS etc. ) -> Block IO -> equipment ...