WPF中的頁面跳轉分爲window頁面跳轉和page頁面跳轉兩類。window爲頂級窗體,其中可以包含frame和page。而frame和page可以相互包含。
window中的頁面跳轉一般是創建一個窗體然後調用show方法。
Page的頁面跳轉根據其宿主元素一般分爲navigationiwindow和frame兩種方式。
其中frame爲輕量級元素,可以嵌入page中間。
而Navigationwindow爲頂級窗口不可以包含於其他元素中間。而且navigationwindow有導航欄,類似於瀏覽器的導航界面,可以記錄歷史導航。但只提供跳轉功能而不保存狀態信息,可以通過該類提供的方法來保存狀態信息。
前臺跳轉:
<Button Height="35" Width="60" >
<Hyperlink x:Name="Link1" NavigateUri="Page1.xaml">Page1</Hyperlink>
</Button> //前臺跳轉
<Button Height="35" Width="60" Margin="228,218,229,66" >
<Hyperlink x:Name="Link2" Click="Link2_Click">Pag2</Hyperlink>
</Button>
<Button Height="35" Width="60" Margin="228,68,229,216" Click="Button_Click">
page1
</Button>
<Frame x:Name="Nav" Width="517" HorizontalAlignment="Right" ></Frame>
用Hyperlink的方式直接跳轉至目標頁面。
後臺跳轉:
1、用Hyperlink進行跳轉
Hyperlink實現了NavigateUri 屬性,該屬性是使用單擊超鏈接時應導航到的內容的 Uri進行設置的。但是只有在Hyperlink的直接或間接父項是導航宿主(包括 NavigationWindow、Frame)
①利用frame進行跳轉
private void Link2_Click(object sender, RoutedEventArgs e)
{
Page2 p2 = new Page2();
Nav.Content = p2;
}
Frame 是能夠定位到並顯示內容的內容控件。 Frame 可以在其他內容中承載,與其他控件和組件。
②也用Content直接創建
this.Content = new Page2();
③使用navigationservice類
1、
Page2 page = new Page2();
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Navigate(page);
、
2、
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Source = new Uri("Page2.xaml", UriKind.Relative);
3、
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Content = new Page2();
4、
Page2 page = new Page2();
this.NavigationService.Navigate(page);
2、用frame進行導航
private void Button_Click(object sender, RoutedEventArgs e)
{
this.pc.Navigate(new Uri("Page1.xaml",UriKind.Relative));
}
3、用Navigationwindow類進行頁面跳轉
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationWindow wds = new NavigationWindow();
wds.Source = new Uri("Page1.xaml", UriKind.Relative);
wds.Show();
}
NavigationWindow 派生自 Window ,擴展了導航和顯示內容的功能。 因爲其前後導航的功能,使得導航更加便捷,因此這個框架也是我最喜歡使用的方式。
以下介紹一些簡單概念:
概念內容文檔參考:
Page
Page 封裝一頁可導航的內容,並具有以下關鍵成員:
生存期管理:KeepAlive。
導航:NavigationService。
外觀:Background、Content、FontFamily、FontSize、Foreground、ShowsNavigationUI、Template、Title。
宿主窗口外觀:WindowHeight、WindowWidth、WindowTitle。
可以使用標記、標記和代碼隱藏或者代碼來定義頁。
NavigationWindow
NavigationWindow 派生自Window,擴展了導航和顯示內容的功能。
內容可以是任何 .NET Framework 對象或 HTML 文件。但是,一般而言,Page 對象是將內容打包以便導航的首選方式。
通過用所需內容的 URI 來設置Source 屬性,可以導航到內容。
Frame
Frame 是能夠定位到並顯示內容的內容控件。 Frame 可以在其他內容中承載,與其他控件和組件。
當 Frame 控件導航到 HTML 內容時,內部Frame 控件實例化本機 webbrowser Activex 控件。WPF
通過對函數控件啓用安全功能在瀏覽器 Activex 控件。應用於的功能控制對 XBAP 和獨立應用程序不同。
某些應用程序應通過附加功能控制防止惡意內容運行。有關更多信息,請參見中的 “瀏覽器控件和功能控制”在安全性 (WPF)
的和WebBrowser Control Overviews and Tutorials部分。內容可以是任何類型的 .NET Framework 對象和 HTML 文件。但是,通常,頁是首選該方法可導航的內容打包 (請參見Page)。
內容可以導航到通過設置與 URI 的 Source 屬性所需內容的。另外,可以使用Navigate 方法的以下重載之一,內容可以導航到:
Navigate(Uri)
Navigate(Uri, Object)