windows phone 8 使用页面传对象的方式 实现页面间的多值传递

来源:互联网 发布:拉什福德数据 编辑:程序博客网 时间:2024/06/05 01:11

           在做windows phone 开发的时候,会经常碰到页面间之间的跳转和传递数据,如果传递的值不多,只有两三个,我们通常使用NavigationService.Navigate(new Uri("页面名? Name=“”&ID=“ ”, UriKind.Relative));

  要是碰到要传递多个值的时候,你就要写很多很多的&符号连接。这样不好看。

这里我写一种更好的方法---传递对象

    思路

         定义一个类,类里面包含你所有需要传递的字段,然后在App.xaml.cs 申明这个类的对象,最后使用这个对象,

向这个对象中添加字段。

做法

第一步:

定义一个公共访问类

public  class GoodHelper    {                public string GoodName        {            get;            set;        }        public int GoodPrice        {            get;            set;        }}

第二步:在App.xaml.cs 中申明这个类的对象

public   static  GoodHelper goodhelper { get; set; }

第三步:在页面响应事件中向这个对象中的字段赋值

 private void  button1_click(object sender, System.Windows.Input.GestureEventArgs e)        {      App.goodhelper = new GoodHelper                {             GoodName="123",             GoodPrice="4576"    }; this.NavigationService.Navigate(new Uri("/market/DetailGood.xaml", UriKind.Relative));   }


最后在跳转到的页面中获取对象的值


  if (App.goodhelper != null)            {                string GoodName = App.goodhelper.GoodName;                int GoodPrice = App.goodhelper.GoodPrice;             }




0 0
原创粉丝点击