WPF学习之页间导航与页间数据传递 |
WPF学习之页间导航与页间数据传递
在WPF中能够很是简单的实现窗口页间的导航:这里有三种方法:web 一、Page调用NavigationService.Navigate函数 新建项目,并新建一个NavigationWindow的窗体做为主窗体,并将Source指向page1.xamlpost 而后新建一个页面page1,(作为主窗体的主题内容。) 在page1上方一个button1学习 而后新建另外一个页面page2,在page2上放一个button2,ui 在button1 的click事件中就能够写实现转向的方法:this Page2 p=new Page2();spa this.NavigationService.Navigate(page2);日志 以上两行代码等价于orm this.NavigationService.Navigate(new uil("page2.xaml",UriKind.Relative));对象 二、xaml中使用Hyperlink标签: <Hyperlink NavigateUri="page2.xaml"> 三、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(); } } 2、经过构造函数传递参数(最易使用) 首先new一个page对象并用NavigationService.Navigate转向 Page2 page2 = new Page2("frompage1"); this.NavigationService.Navigate(page2); 在page2中定义构造函数 public Page2(string args) { string args2 = args; }
3、经过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"]; } |