当前位置: 移动技术网 > IT编程>移动开发>WP > windows phone:页面间数据共享

windows phone:页面间数据共享

2018年09月29日  | 移动技术网IT编程  | 我要评论

内蒙古晨报,君有芊芊劫,今年立秋是几点

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网