在wpf項目中,經常遇到需要跳轉視窗的功能,在以前分享一篇了
今天在分享一段代碼,是通過content進行頁面跳轉的,這個和web的跳轉就一點都不一樣了。
介面:
點擊menu1 和2都會跳轉到Page1.xaml和 Page2.xaml
前台xaml:
| 代碼如下 |
複製代碼 |
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel Width="150" HorizontalAlignment="Left"> <TextBlock FontSize="24" TextWrapping="Wrap"> <Hyperlink x:Name="LnkPre" Foreground="Black" Click="LnkPre_Click"> Menu1 </Hyperlink> </TextBlock> <TextBlock FontSize="24" TextWrapping="Wrap"> <Hyperlink x:Name="LnkPre1" Foreground="Black" Click="LnkPre1_Click"> Menu2 </Hyperlink> </TextBlock> </StackPanel> <Frame Name="pc" Width="340" HorizontalAlignment="Right"> </Frame> </Grid> </Window> |
後台代碼跳轉:
| 代碼如下 |
複製代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication3 { /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); pc.NavigationUIVisibility = NavigationUIVisibility.Hidden; } private void LnkPre1_Click(object sender, RoutedEventArgs e) { Page2 p2 = new Page2(); pc.Content = p2; } private void LnkPre_Click(object sender, RoutedEventArgs e) { Page1 p1 = new Page1(); pc.Content = p1; } } } |