WPF 基于导航的Windows应用程序
2011-08-22 11:30 178人阅读 评论(0) 收藏 举报
- 在WPF中使用导航,内容被组织在Page元素中,Page能寄宿在NavigationWindow或者Frame。
这些容器能提供一种从页到页的导航,一本记录所有导航的日志,及一系列导航相关事件。
- <NavigationWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- x:Class="WPF_Test.MainWindow"
- Title="窗体" Source="Page1.xaml">
- </NavigationWindow>
页间导航:
1.调用Navigate方法
- //Page1.xaml导航到Page2.xaml
- Page2 p2 = new Page2();
- NavigationService.Navigate(p2);
- //NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
2.使用Hyperlinks(TextBlock标签内)
- <TextBlock>
- <Hyperlink NavigateUri="Page2.xaml">点击这里^-^</Hyperlink>
- </TextBlock>
3.使用导航日志
每一个导航容器包含记录导航历史信息的日志,导航日志提供了后退与前进的逻辑。
NavigationWindow总有一个导航日志,Frame可能没有,但可通过JournalOwnership属性设置,可通过NavigationUIVisibility设置false,隐藏导航按钮。
- //如果能后退
- if (NavigationService.CanGoForward)
- {
- NavigationService.GoForward();
- }
- else
- {
- NavigationService.GoBack();
- }
页间数据传递:
1.向页面传送数据
- //定义一个参数
- NameValueCollection nvc = new NameValueCollection();
- nvc["num"] = "lulu";
- nvc["num2"] = "66";
- //实例化要导航到的页面
- Page2 p2 = new Page2();
- //订阅导航LoadCompleted事件,添加自己的处理方法用于接参数
- NavigationService.LoadCompleted += p2.OnLoadCompleted;
- //Page1.xaml导航到Page2.xaml,传递一个nvc参数
- NavigationService.Navigate(p2, nvc);
- //Page2.xaml.cs中定义获取传入参数
- public void OnLoadCompleted(object sender, NavigationEventArgs e)
- {
- if (e.ExtraData != null)
- {
- NameValueCollection nvc = e.ExtraData as NameValueCollection;
- //用获取到的参数重新设置Label的值
- Label1.Content = nvc[0];
- }
- }
2.为页面添加一带参数构函,实现页间传值
- //实例化要导航到的页面
- Page2 p2 = new Page2("来自Page1.xaml的参数");
- //Page1.xaml导航到Page2.xaml
- NavigationService.Navigate(p2);
- //定义含参数构函,可用于接收其他页面传递信息
- public Page2(string value):this()
- {
- Label1.Content = value
- }
3.Application对象Properties集合全局共享数据
- Application.Current.Properties["globe"] = "globe";
PageFunction从页面返回数据:
处理让用户导航到某页,做一些操作,完毕自动返回前一页,避免导航日志中的不良后果。
- PageFunction1 pgf = new PageFunction1();
- //处理PageFunction的Return事件
- pgf.Return += OnReturn;
- //Page1.xaml导航到Page2.xaml
- NavigationService.Navigate(pgf);
- //处理PageFunction1页面返回值
- private void OnReturn(object sender, ReturnEventArgs<string> e)
- {
- //用返回结果重新设置Button2内容值
- Button2.Content = e.Result;
- }
- /* PageFunction<string>为一泛型,String为返回数据类型 */
- public partial class PageFunction1 : PageFunction<String>
- {
- public PageFunction1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- //表示处理完成,将当前TextBox1的值传回
- this.OnReturn(new ReturnEventArgs<string>(this.TextBox1.Text));
- }
- }</string>
自定义对话框:
- <Window x:Class="WPF_Test.MyDialog"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MyDialog" Height="148" Width="280">
- <Grid>
- <Label Content="你的名字:" Height="28" HorizontalAlignment="Left" Margin="21,9,0,0" Name="label1" VerticalAlignment="Top" Width="108" />
- <TextBox Height="23" Margin="21,35,27,0" Name="textBox1" VerticalAlignment="Top" />
- <Button Content="确 定" Height="23" HorizontalAlignment="Left" Margin="37,70,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
- <Button Content="取 消" Height="23" HorizontalAlignment="Right" Margin="0,70,41,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
- </Grid>
- </Window>
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- //窗体显示成功,自动关闭
- this.DialogResult = true;
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- //窗体显示失败
- this.DialogResult = false;
- }
调用自定义对话框
- MyDialog md = new MyDialog();
- md.Title = "自定义对话框";
- md.WindowStartupLocation = WindowStartupLocation.CenterOwner;
- //以对话框模式阻塞显示窗口,ShowDialog返回一bool
- if (md.ShowDialog() == true)
- {
- //((Button)sender).Foreground = Brushes.Coral;
- }