WPF學習之頁間導航與頁間數據傳遞

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"];

     }

相關文章
相關標籤/搜索