导航应用程序演绎 1.将导航的内容寄宿于窗口中 2.Xaml浏览器应用程序 扩展名.xbap 可以直接在浏览器中运行.
4个核心 1.页面:WPF将导航内容封装为多个页面. 2.超链接 3.NavigationServices 4.Journal 每次连接通过Jourmal记录作为历史记录.
Page的宿主窗口 Page的宿主窗口包括浏览器,导航窗口和Frame. NavigationWindow是一个顶层窗口,不允许嵌入到其他控件中,而Frame则为轻量级,可以嵌入到其他控件,如NavigationWindow或者Page,甚至其他Frame中.Frame默认没有导航工具栏,可以设置其NavigationUIVisiblity属性为Visible使其工具栏是否可见.
导航链接 超链接 1 2 <HyperLink NavigateUri="Page2.xaml" >开始阅读路由事件 </HyperLink>
段落导航 NavigateUri的设置方法是”页面名#元素名” HyperLink Click NavigationService Navigate方法导航.
1 2 3 private Hyperlink_Click(object sender,RoutedEventArgs e){ NavigationService.Navigate(newUri("pack://application:,,,/Page2.xaml" )); }
其他方式导航 NavigationCommands.BrowseBack NavigationCommands.BrowseForward
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 <Page x:Class="WpfApp1.Pages.LoginPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local ="clr-namespace:WpfApp1.Pages" Title="登陆页面" ShowsNavigationUI="True" WindowTitle="登录页面" Loaded="Page_Loaded" PreviewLostKeyboardFocus="Page_PreviewLostKeyboardFocus" > <Border BorderBrush="Black" BorderThickness="2" Margin="10" > <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition Height="auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="4*" /> <ColumnDefinition Width="6*" /> </Grid.ColumnDefinitions> <TextBlock Text="用户名" Margin="5" /> <TextBox Style="{x:Null}" x:Name="name" Grid.Column="1" Margin="5" /> <TextBlock Grid.Row="1" Text="密码" Margin="5" /> <PasswordBox x:Name="password" Grid.Row="1" Grid.Column="1" Margin="5" /> <Button x:Name="btn" Grid.Row="2" Height="25" Width="80" HorizontalAlignment="Left" Margin="5 0 0 0" Click="btn_Click" Content="登录" /> <TextBlock Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" > <Hyperlink NavigateUri="RegisterPage.xaml" > 忘记密码了... ... </Hyperlink> </TextBlock> <TextBlock Margin="0" Grid.Row="3" Grid.ColumnSpan="2" x:Name="hyperlinktext" HorizontalAlignment="Center" VerticalAlignment="Center" > 如果没有注册,请单击 <Hyperlink Click="Hyperlink_Click" > 注册 </Hyperlink> 页面 <LineBreak/> </TextBlock> </Grid> </Border> </Page> public partial class LoginPage : Page { public LoginPage () { InitializeComponent(); } public string FocusElement { get { return (string)GetValue(FocusElementProperty); } set { SetValue(FocusElementProperty, value); } } // Using a DependencyProperty as the backing store for FocusElement. This enables animation, styling, binding, etc... public static readonly DependencyProperty FocusElementProperty = DependencyProperty.Register("FocusElement" , typeof(string), typeof(LoginPage), new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.Journal)); private void btn_Click(object sender, RoutedEventArgs e) { List<User> users =((App) App.Current).users ; int usercount = users.Count; User user = new User(name.Text,password.ToString()); for (int i = 0; i < usercount; i++) { if (name.Text == users [i].Name && password.Password == users [i].Password) { WelcomePage page = new WelcomePage(user,false ); NavigationService.Navigate(page); return ; } } NavigationService.Navigate(new Uri("pack://application:,,,/Pages/ErrorPage.xaml" )); } private void Page_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (e.NewFocus==this.name || e.NewFocus==this.password) { this.FocusElement=(string)(((DependencyObject)e.NewFocus).GetValue(FrameworkElement.NameProperty)); } } private void Page_Loaded(object sender, RoutedEventArgs e) { if (this.FocusElement != null) { IInputElement element = (IInputElement)LogicalTreeHelper.FindLogicalNode(this,this.FocusElement); Keyboard.Focus(element); } } private void Hyperlink_Click(object sender, RoutedEventArgs e) { RegisterPage registerPage = new RegisterPage(); registerPage.Return += RegisterPage_Return; this.NavigationService.Navigate(registerPage); } private void RegisterPage_Return(object sender, ReturnEventArgs<User> e) { if (e == null) { return ; } User user = e.Result; if (user != null) { this.name.Text = user.Name; this.password.Password = password.Password; } List<User> list = ((App)(App.Current)).users ; list.Add(user); } } <PageFunction x:Class="WpfApp1.Pages.RegisterPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local ="clr-namespace:WpfApp1.Pages" xmlns:bookmodels="clr-namespace:BookModels;assembly=BookModels" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Title="RegisterPage" x:TypeArguments="bookmodels:User" > <Border BorderBrush="Black" BorderThickness="2" > <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition Height="auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="4*" /> <ColumnDefinition Width="7*" /> </Grid.ColumnDefinitions> <TextBlock Text="用户名" Margin="5" /> <TextBox x:Name="name" Grid.Column="1" Margin="5" /> <TextBlock Grid.Row="1" Text="密码" Margin="5" /> <PasswordBox x:Name="password" Grid.Row="1" Grid.Column="1" Margin="5" /> <TextBlock Grid.Row="2" Text="再次输入密码" Margin="5" /> <PasswordBox x:Name="Secondpassword" Grid.Row="2" Grid.Column="1" Margin="5" /> <StackPanel Orientation="Horizontal" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" > <Button Content="注册" Margin="30 0 0 0" Click="Button_Click" /> <Button Content="取消" Margin="30 0 30 0" Command="Close" /> </StackPanel> </Grid> </Border> </PageFunction> public partial class RegisterPage : PageFunction<User> { public RegisterPage () { InitializeComponent(); user = new User(); } private User user; private void Button_Click(object sender, RoutedEventArgs e) { User user = CreateUser(); if (user == null) return ; else OnReturn(new ReturnEventArgs<User>(user)); } private User CreateUser () { if (string.IsNullOrEmpty(name.Text) || string.IsNullOrEmpty(password.Password)|| string.IsNullOrEmpty(Secondpassword.Password) || password.Password!=Secondpassword.Password) { BiaoZhuWindow biaoZhu=new BiaoZhuWindow(); biaoZhu.mes.Content = "用户名或密码有误!" ; biaoZhu.Show(); return null; } user = new User () { Name=name.Text,Password=Secondpassword.Password}; return user; } }