WPF学习之页间导航与页间数据传递

WPF学习之页间导航与页间数据传递
 
 

 

WPF学习之页间导航与页间数据传递 

在WPF中可以非常简单的实现窗口页间的导航:这里有三种方法:

1、Page调用NavigationService.Navigate

新建项目,并新建一个NavigationWindow的窗体作为主窗体,并将Source指向page1.xaml

然后新建一个页面page1,(做为主窗体的主题内容。) 在page1上方一个button1

然后新建另一个页面page2,在page2上放一个button2,

在button1 的click事件中就可以写实现转向的方法:

Page2 p=new Page2();

this.NavigationService.Navigate(page2);

以上两行代码等价于

this.NavigationService.Navigate(new uil("page2.xaml",UriKind.Relative));

2、xaml中使用Hyperlink标签:

<Hyperlink NavigateUri="page2.xaml">

3、NavigationWindow的导航日志

返回上一页:this.NavigationService.GoBack

 向前下一页:this.NavigationService.GoForward

 

 已上是页面之间导航,同时在转向的同时最常见的就是要数据传递:

 一.通过NavigationService.Navigate传递参数

  1.首先new一个page对象:

          Page2 page2 = new Page2();

  2.通过NavigationService.Navigate传递参数

          this.NavigationService.Navigate(page2,"frompage1");

  3.在Page2,处理NavigationWindow的LoadCompleted事件

          this.NavigationService.LoadCompleted += new LoadCompletedEventHandler(page2_LoadCompleted);

  4.在page2_LoadCompleted方法里获取传递过来的参数       

          void page2_LoadCompleted(object sender,NavigationEventArgs e)

    {

      if(e.ExtraData != null)

      {

                     string args = e.ExtraData.toString();

      }

          }

 二、通过构造函数传递参数(最易使用)

  首先new一个page对象并用NavigationService.Navigate转向

    Page2 page2 = new Page2("frompage1");

    this.NavigationService.Navigate(page2);

     在page2中定义构造函数

    public Page2(string args)

    {

      string args2 = args;

    }

 

 三、通过Application.Properties的全局数据

   用实例或URI导航到页面

          Application.Properties["Args"] = "frompage2";

     Page2 page2 = new Page2();

          this.NavigationService.Navigate(page2);

   在page2页面检查参数值

              if(Application.Properties["Args"] != null)

     {

      string arg = Application.Properties["Args"];

     }

posted @ 2013-07-26 14:53  greefsong  阅读(1873)  评论(0编辑  收藏  举报