在WPF中使用導航,內容被組織在Page元素中,Page能寄宿在NavigationWindow或者Frame。
這些容器能提供一種從頁到頁的導航,一本記錄所有導航的日誌,及一系列導航相關事件。
<NavigationWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WPF_Test.MainWindow" Title="表單" Source="Page1.xaml"> </NavigationWindow>
頁間導航:
1.調用Navigate方法
//Page1.xaml導航到Page2.xamlPage2 p2 = new Page2();NavigationService.Navigate(p2);//NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
2.使用Hyperlinks(TextBlock標籤內)
<TextBlock> <Hyperlink NavigateUri="Page2.xaml">點擊這裡^-^</Hyperlink></TextBlock>
3.使用導航日誌
每一個導航容器包含記錄導航曆史資訊的日誌,導航日誌提供了後退與前進的邏輯。
NavigationWindow總有一個導航日誌,Frame可能沒有,但可通過JournalOwnership屬性設定,可通過NavigationUIVisibility設定false,隱藏導覽按鈕。
//如果能後退if (NavigationService.CanGoForward){ NavigationService.GoForward();}else{ NavigationService.GoBack();}
頁間資料傳遞:
1.向頁面傳送資料
//定義一個參數NameValueCollection nvc = new NameValueCollection();nvc["num"] = "lulu";nvc["num2"] = "66";//執行個體化要導航到的頁面Page2 p2 = new Page2();//訂閱導航LoadCompleted事件,添加自己的處理方法用於接參數NavigationService.LoadCompleted += p2.OnLoadCompleted;//Page1.xaml導航到Page2.xaml,傳遞一個nvc參數NavigationService.Navigate(p2, nvc);
//Page2.xaml.cs中定義擷取傳入參數public void OnLoadCompleted(object sender, NavigationEventArgs e){ if (e.ExtraData != null) { NameValueCollection nvc = e.ExtraData as NameValueCollection; //用擷取到的參數重新設定Label的值 Label1.Content = nvc[0]; }}
2.為頁面添加一帶參數構函,實現頁間傳值
//執行個體化要導航到的頁面Page2 p2 = new Page2("來自Page1.xaml的參數");//Page1.xaml導航到Page2.xamlNavigationService.Navigate(p2);
//定義含參數構函,可用於接收其他頁面傳遞資訊public Page2(string value):this(){ Label1.Content = value}
3.Application對象Properties集合全域共用資料
Application.Current.Properties["globe"] = "globe";
PageFunction從頁面返回資料:
處理讓使用者導航到某頁,做一些操作,完畢自動返回前一頁,避免導航日誌中的不良後果。
PageFunction1 pgf = new PageFunction1();//處理PageFunction的Return事件pgf.Return += OnReturn;//Page1.xaml導航到Page2.xamlNavigationService.Navigate(pgf);//處理PageFunction1頁面傳回值private void OnReturn(object sender, ReturnEventArgs<string> e){ //用返回結果重新設定Button2內容值 Button2.Content = e.Result;}
/* PageFunction為一泛型,String為返回資料類型 */public partial class PageFunction1 : PageFunction<String>{ public PageFunction1() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { //表示處理完成,將當前TextBox1的值傳回 this.OnReturn(new ReturnEventArgs<string>(this.TextBox1.Text)); }}
自訂對話方塊:
<Window x:Class="WPF_Test.MyDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MyDialog" Height="148" Width="280"> <Grid> <Label Content="你的名字:" Height="28" HorizontalAlignment="Left" Margin="21,9,0,0" Name="label1" VerticalAlignment="Top" Width="108" /> <TextBox Height="23" Margin="21,35,27,0" Name="textBox1" VerticalAlignment="Top" /> <Button Content="確 定" Height="23" HorizontalAlignment="Left" Margin="37,70,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <Button Content="取 消" Height="23" HorizontalAlignment="Right" Margin="0,70,41,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> </Grid></Window>
private void button1_Click(object sender, RoutedEventArgs e){ //表單顯示成功,自動關閉 this.DialogResult = true;}private void button2_Click(object sender, RoutedEventArgs e){ //表單顯示失敗 this.DialogResult = false;}
調用自訂對話方塊
MyDialog md = new MyDialog();md.Title = "自訂對話方塊";md.WindowStartupLocation = WindowStartupLocation.CenterOwner;//以對話方塊模式阻塞顯示視窗,ShowDialog返回一boolif (md.ShowDialog() == true){ //((Button)sender).Foreground = Brushes.Coral;}