欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

windows phone:页面间数据共享,phone数据共享

来源: 开发者 投稿于  被查看 42350 次 评论:155

windows phone:页面间数据共享,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)

        {  www.Bkjia.com

            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类所定义的属性在这里相当于一个全局变量,整个应用程序皆可访问。

相关频道:

用户评论