windows phone:页面间数据共享

可以通过静态属性Application.Current可以返回当前程序的Application对象,然后可以简

单地将其转换成App类型。这意味着可以使用App类来存储用于程序中多个页面共享的数据。
下面例子演示如何利用App类实现页面间数据共享:

在Silverlight项目的App类定义一个简单的公共属性:

public partial class App : Application
{
    //用于在页面间共享数据的公共属性
    public Color? SharedColor { set; get; }//这个属性定义为可空的(nullable)Color

对象,而不仅仅是一般的Color对象
    ...
}

源页面MainPage如下所示:

MainPage.xaml中包含TextBlock:

<TextBlock HorizontalAlignment="Center" Name="txt1" Text="navigate to 2nd page" VerticalAlignment="Center" ManipulationStarted="txt1_ManipulationStarted" />

MainPage.xaml.cs代码如下:

namespace PhoneApp2
{
    public partial class MainPage : PhoneApplicationPage
    {
        Random rand = new Random();
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            String destination = "/Page1.xaml";
            if (this.ContentPanel.Background is SolidColorBrush)
            {
                (Application.Current as App).SharedColor = (this.ContentPanel.Background as SolidColorBrush).Color;//在导航到Page1页面之前首先将Color对象保存到App类的属性中
            }
            this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));//导航至指定页面
            e.Complete();
            e.Handled = true;
        }
        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            //当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
            this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
            base.OnManipulationStarted(e);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//该函数被调用时,页面的构造函数已经执行完毕,但还没有执行其他的方法
        {
            Color? sharedColor = (Application.Current as App).SharedColor;//访问公共属性
            if (sharedColor != null)
            {
                this.ContentPanel.Background = new SolidColorBrush(sharedColor.Value);
            }
            base.OnNavigatedTo(e);
        }
    }
}

目标页面Page1如下所示:

Page1.xaml中包含TextBlock:

<TextBlock HorizontalAlignment="Left" Margin="157,212,0,0" Name="txt2" Text="go back to 1st page" VerticalAlignment="Top" ManipulationStarted="txt2_ManipulationStarted" />

Page1.xaml.cs代码如下所示:

namespace PhoneApp2
{
    public partial class Page1 : PhoneApplicationPage
    {
        Random rand = new Random();
        public Page1()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//该函数被调用时,页面的构造函数已经执行完毕,但还没有执行其他的方法
        {
            Color? shareColor = (Application.Current as App).SharedColor;//访问公共属性
            if (shareColor != null)
            {
                this.ContentPanel.Background = new SolidColorBrush(shareColor.Value);
            }
            base.OnNavigatedTo(e);
        }
        private void txt2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            if (this.ContentPanel.Background is SolidColorBrush)
            {
                (Application.Current as App).SharedColor = (this.ContentPanel.Background as SolidColorBrush).Color;//在返回到MainPage页面之前将当前Color对象保存到App类的属性中
            }
            this.NavigationService.GoBack();
            e.Complete();
            e.Handled = true;
        }
        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            //当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
            this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
            base.OnManipulationStarted(e);
        }

    }
}

运行程序你就会发现在页面之间进行导航时,页面总是共享相同的颜色。

其实你在App类所定义的属性在这里相当于一个全局变量,整个应用程序皆可访问。

 

你可以通过静态属性PhoneApplicationService.Current来获取现有PhoneApplicationService的实例,如下所示:

PhoneApplicationService appService = PhoneApplicationService.Current;

PhoneApplicationService类定义在命名空间Microsoft.Phone.Shell中,它的实例在标准的App.xaml文件中创建:

<Application.ApplicationLifetimeObjects>
        <!--处理应用程序的生存期事件所需的对象-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>

 </Application.ApplicationLifetimeObjects>

PhoneApplicationService包含一个State属性(类型为IDictionary<String,object>)可用于保存和恢复数据。但该State字典只适用于存储程序同一次运行过程中的临时数据。如果需要在程序多次执行过程之间保存数据,建议使用独立存储。访问State属性内容类似下面所示:

Color clr = (Color)appService.State["Color"];

appService.State["Color"] = clr;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值