在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; } } } |
时间: 2024-10-26 01:51:34