WPF 基于导航的Windows应用程序

178人阅读 评论(0) 收藏 举报
  1. 在WPF中使用导航,内容被组织在Page元素中,Page能寄宿在NavigationWindow或者Frame。  

这些容器能提供一种从页到页的导航,一本记录所有导航的日志,及一系列导航相关事件。

  1. <NavigationWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  3.     x:Class="WPF_Test.MainWindow"  
  4.     Title="窗体" Source="Page1.xaml">   
  5. </NavigationWindow>  

 

页间导航:

1.调用Navigate方法

  1. //Page1.xaml导航到Page2.xaml   
  2. Page2 p2 = new Page2();  
  3. NavigationService.Navigate(p2);  
  4. //NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));  

2.使用Hyperlinks(TextBlock标签内)

  1. <TextBlock>  
  2.     <Hyperlink NavigateUri="Page2.xaml">点击这里^-^</Hyperlink>  
  3. </TextBlock>  

 

3.使用导航日志

每一个导航容器包含记录导航历史信息的日志,导航日志提供了后退与前进的逻辑。

NavigationWindow总有一个导航日志,Frame可能没有,但可通过JournalOwnership属性设置,可通过NavigationUIVisibility设置false,隐藏导航按钮。

  1. //如果能后退   
  2. if (NavigationService.CanGoForward)  
  3. {  
  4.     NavigationService.GoForward();  
  5. }  
  6. else  
  7. {  
  8.     NavigationService.GoBack();  
  9. }  

 

页间数据传递:

1.向页面传送数据

  1. //定义一个参数   
  2. NameValueCollection nvc = new NameValueCollection();  
  3. nvc["num"] = "lulu";  
  4. nvc["num2"] = "66";  
  5.   
  6. //实例化要导航到的页面   
  7. Page2 p2 = new Page2();  
  8. //订阅导航LoadCompleted事件,添加自己的处理方法用于接参数   
  9. NavigationService.LoadCompleted += p2.OnLoadCompleted;  
  10.   
  11. //Page1.xaml导航到Page2.xaml,传递一个nvc参数   
  12. NavigationService.Navigate(p2, nvc);  

 

  1. //Page2.xaml.cs中定义获取传入参数   
  2. public void OnLoadCompleted(object sender, NavigationEventArgs e)  
  3. {  
  4.     if (e.ExtraData != null)  
  5.     {  
  6.         NameValueCollection nvc = e.ExtraData as NameValueCollection;  
  7.         //用获取到的参数重新设置Label的值   
  8.         Label1.Content = nvc[0];  
  9.     }  
  10. }  


2.为页面添加一带参数构函,实现页间传值

  1. //实例化要导航到的页面   
  2. Page2 p2 = new Page2("来自Page1.xaml的参数");  
  3. //Page1.xaml导航到Page2.xaml   
  4. NavigationService.Navigate(p2);  
  1. //定义含参数构函,可用于接收其他页面传递信息   
  2. public Page2(string value):this()  
  3. {  
  4.     Label1.Content = value  
  5. }  


3.Application对象Properties集合全局共享数据

  1. Application.Current.Properties["globe"] = "globe";  


PageFunction从页面返回数据:

处理让用户导航到某页,做一些操作,完毕自动返回前一页,避免导航日志中的不良后果。

  1. PageFunction1 pgf = new PageFunction1();  
  2. //处理PageFunction的Return事件   
  3. pgf.Return += OnReturn;  
  4. //Page1.xaml导航到Page2.xaml   
  5. NavigationService.Navigate(pgf);  
  6.   
  7. //处理PageFunction1页面返回值   
  8. private void OnReturn(object sender, ReturnEventArgs<string> e)  
  9. {  
  10.     //用返回结果重新设置Button2内容值   
  11.     Button2.Content = e.Result;  
  12. }  
  1. /* PageFunction<string>为一泛型,String为返回数据类型 */  
  2. public partial class PageFunction1 : PageFunction<String>  
  3. {  
  4.     public PageFunction1()  
  5.     {  
  6.         InitializeComponent();  
  7.     }  
  8.   
  9.     private void button1_Click(object sender, RoutedEventArgs e)  
  10.     {  
  11.         //表示处理完成,将当前TextBox1的值传回   
  12.         this.OnReturn(new ReturnEventArgs<string>(this.TextBox1.Text));  
  13.     }  
  14. }</string>  


自定义对话框:

  1. <Window x:Class="WPF_Test.MyDialog"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MyDialog" Height="148" Width="280">  
  5.     <Grid>  
  6.         <Label Content="你的名字:" Height="28" HorizontalAlignment="Left" Margin="21,9,0,0" Name="label1" VerticalAlignment="Top" Width="108" />  
  7.         <TextBox Height="23" Margin="21,35,27,0" Name="textBox1" VerticalAlignment="Top" />  
  8.         <Button Content="确 定" Height="23" HorizontalAlignment="Left" Margin="37,70,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />  
  9.         <Button Content="取 消" Height="23" HorizontalAlignment="Right" Margin="0,70,41,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />  
  10.     </Grid>  
  11. </Window>  
  1. private void button1_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     //窗体显示成功,自动关闭   
  4.     this.DialogResult = true;  
  5. }  
  6.   
  7. private void button2_Click(object sender, RoutedEventArgs e)  
  8. {  
  9.     //窗体显示失败   
  10.     this.DialogResult = false;  
  11. }  

调用自定义对话框

  1. MyDialog md = new MyDialog();  
  2. md.Title = "自定义对话框";  
  3. md.WindowStartupLocation = WindowStartupLocation.CenterOwner;  
  4.   
  5. //以对话框模式阻塞显示窗口,ShowDialog返回一bool   
  6. if (md.ShowDialog() == true)  
  7. {  
  8.     //((Button)sender).Foreground = Brushes.Coral;   
  9. }  



posted @ 2012-02-12 20:54  therockthe  阅读(639)  评论(0编辑  收藏  举报