最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
windows phone 数据共享的几种方式
时间:2022-06-26 00:19:14 编辑:袖梨 来源:一聚教程网
本节实现的内容是数据共享,实现的效果描述:首先是建立两个页面,当页面MainPage通过事件导航到页面SecondPage是,我们需要将MainPage中的一些内容(比如一个字符串)传递到SecondPage中,SecondPage页面就出呈现出传递来的内容,当页面SecondPage通过事件导航到页面MainPage的时候,我们也把一些内容(比如一个字符串)传递与页面MainPage;
代码如下 | 复制代码 |
using Microsoft.Phone.Shell;
MainPage的隐藏文件的全部代码如下:
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; //引用命名空间--PhoneApplicationService类用到 using Microsoft.Phone.Shell; namespace ShareData { public partial class MainPage : PhoneApplicationPage { // 构造函数 public MainPage() { InitializeComponent(); } /// /// 点击导航到第二个页面 /// /// /// private void btn_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new Uri("/SecondPage.xaml",UriKind.Relative)); } //知识点① protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { //目标页--知识点② if (e.Content is SecondPage) { ((SecondPage)e.Content).ApplicationTitle.Text = "传递数据成功!"; } //获得application对象的引用--知识点③ (Application.Current as App).shareData = "通过APP类的属性共享数据"; //应用程序的状态管理---知识点④ PhoneApplicationService.Current.State["Share"] = "临时数据"; base.OnNavigatedFrom(e); } ///// ///// 接受传递的值 ///// ///// //protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) //{ // //获得App类中的共享数据 // PageTitle.Text = (Application.Current as App).shareData.ToString(); // if (PhoneApplicationService.Current.State.ContainsKey("Share")) // { // //获得phoneapplicationService对象中设置state属性 // PageTitle.Text += "n" + PhoneApplicationService.Current.State["Share"].ToString(); // } // base.OnNavigatedTo(e); //} } } |
using Microsoft.Phone.Shell;
代码如下 | 复制代码 |
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; //引用命名空间--PhoneApplicationService类用到 using Microsoft.Phone.Shell; namespace ShareData { public partial class SecondPage : PhoneApplicationPage { public SecondPage() { InitializeComponent(); } /// /// 接受传递的值 /// /// protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { //获得App类中的共享数据 PageTitle.Text = (Application.Current as App).shareData.ToString(); if (PhoneApplicationService.Current.State.ContainsKey("Share")) { //获得phoneapplicationService对象中设置state属性 PageTitle.Text += "n" + PhoneApplicationService.Current.State["Share"].ToString(); } base.OnNavigatedTo(e); } /// /// 导航到第一个页面 /// /// /// 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 = "传递数据成功!"; // } // (Application.Current as App).shareData = "通过APP类的属性共享数据"; // PhoneApplicationService.Current.State["Share"] = "临时数据"; // base.OnNavigatedFrom(e); //} |
public string shareData { get; set; }
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">
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
相关文章
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21
- 《潜行者2:切尔诺贝利之心》使用手电筒方法介绍 11-21
- 《潜行者2:切尔诺贝利之心》回声探测器使用攻略分享 11-21