1.2. Navigation Applications-1.2,导航应用程序

1.2. Navigation Applications

If you create a new WPF application using Visual Studio, you may notice that a few icons down from the Avalon Application icon is another project template called Avalon Navigation Application.

WPF itself was created as a unified presentation framework, meant to enable building Windows applications with the best features from existing Windows application practice and existing web application practice. One of the nice things that web applications generally provide is a single window showing the user one page of content/functionality at a time, allowing for navigation between the pages. For some applications, including Internet Explorer, the Shell Explorer, Microsoft Money and a bunch of Control Panels, this is thought to be preferable to the more common Windows application practice of showing more than one window at a time.

To enable more of these kinds of applications in Windows, WPF provides the NavigationApplication in Example 1-14 to serve as the base of your custom application class instead of the Application class.

--------------------------------------------------------------------------

1.2 导航应用程序

如果您使用Visual Studio建立一个新的WPF应用程序,你可能会注意到在Avalon应用程序图标下面有几个其他的项目模板的图标.他们称为Avalon导航应用程序.

WPF本身是作为一个统一的显示框架而被创建的,旨在为Windows应用程序的构造工作提供现存Windows应用程序和Web应用程序中最卓越的功能.Web应用程序普遍提供的美妙特性中医一项是一个单独的窗口可以在一个页面中为用户呈现内容或功能,并允许用户在不同页面之间导航.对于包括IE,资源管理器,Microsoft Money和部分控制面板项目在内的一些应用程序来说,导航被认为比很多应用程序同时显示多个窗口的做法更为可取.为使Windows中更多此类应用程序能够使用导航功能,WPF提供了NavigationApplication类取代Application类成为了您自己的应用程序类的基类.如例1-14所示.

 

Example 1-14. The C# portion of a navigation application // MyApp.xaml.cs using System; using System.Windows; using System.Windows.Navigation; namespace MyNavApp { public partial class MyApp : NavigationApplication {} }

The NavigationApplication itself derives from the Application class and provides additional services such as navigation, history, and tracking the initial page to show when the application first starts, which is specified in the application/'s XAML file, as in Example 1-15.

------------------------------------------------------------------

NavigationApplication 起源于Application类,并提供了诸如导航,历史,跟踪起始页以显示应用程序启动时间等格外功能.这些在应用程序的XAML文件中都被指定了,如例1-15所示.

 

Example 1-15. The XAML portion of a navigation application <!-- MyApp.xaml.cs --> <NavigationApplication x:Class="MyNavApp.MyApp" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" StartupUri="Page1.xaml"> </NavigationApplication>

In addition to the StartupUri, which specifies the first XAML page to show in our navigation application, notice that the NavigationApplication element doesn/'t have a Text property. In fact, if you were to set one, that would cause a compilation error, because a navigation application/'s main window title is set by the current page. A page in a WPF navigation application is a class that derives from the Page class, e.g., the XAML in Example 1-16.

----------------------------------------------------------------

除了用于指定最初的用于显示我们的导航应用程序的XAML页之外,注意没有Text属性的NavigationApplication元素.事实上,如果你没有这个元素,将会导致一个编译器错误,因为一个导航应用程序主窗口的标题是有当前页决定的.WPF导航应用程序中的页指的是一个继承自Page类的类,例如例1-16中所述的XAML代码.

 

Example 1-16. A sample navigation page <!-- Page1.xaml --> <Page x:Class="MyNavApp.Page1" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" Text="Page 1"> <TextBlock FontSize="72" TextWrap="Wrap"> Check out <Hyperlink NavigateUri ="page2.xaml">page 2</Hyperlink>, too. </TextBlock> </Page>

Remember that the root element of a XAML file defines the base class, so this Page root element defines a class (MyNavApp.Page1) that derives from the WPF Page class. The Text property of the page will be the thing that shows in the caption as the user navigates from page to page.

------------------------------------------------------------------------

记住,XAML文件的根元素决定了基类,因此这里的Page根元素决定了一个继承自WPF Page基类的类.这个页的Text属性将会作为用户在不同页中导航时的标题.

 

1.2.1. Navigation

The primary way to allow the user to navigate is via the Hyperlink element, setting the NavigateUri to a relative URL of another page XAML in the project.

Figure 1-7. A sample navigation page in action

In Figure 1-7, the hyperlinked text is underlined in blue, and if you were to move your mouse cursor over the hyperlink, it would show up as red. Further, the page/'s Text property is set as the window caption, as well as on the toolbar across the top. This toolbar is provided for navigation applications for the sole purpose of providing the back and forward buttons. The act of navigation through the application will selectively enable and disable these buttons, as well as fill in the history drop-down maintained by each button.

Let/'s define page2.xaml, as shown in Example 1-17.

------------------------------------------------------------------------------

1.2.1 导航

最主要的允许用户导航的方法就是通过Hyperlink元素,将NavigateUri属性设置为项目中另一个页的XAML文件的相对路径.

图1-7中,超链接文本有着蓝色下划线,当你的鼠标指针指向超链接时,它将变成红色.本页的Text属性被设置成窗口的标题,顶部的工具栏也一样.这个工具栏唯一的目的就是为导航应用程序提供返回或向前的按钮.应用程序的导航行为将会是这些按钮激活或者歇菜,正如被这些按钮维护的历史列表一样.

定义page2.xaml的代码如例1-17所示.

 

Example 1-17. Another sample navigation page <!-- Page2.xaml --> <Page x:Class="MyNavApp.Page2" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" Text="Page 2"> <TextBlock FontSize="72" TextWrap="Wrap"> Hello, and welcome to page 2. <Button FontSize="72" Click="page1Button_Click">Page 1</Button> <Button FontSize="72" Click="backButton_Click">Back</Button> <Button FontSize="72" Click="forwardButton_Click">Forward</Button> </TextBlock> </Page>

Clicking on the hyperlink on page 1 navigates to page 2, as shown in Figure 1-8.

Figure 1-8. Navigation history and custom navigation controls

Notice in Figure 1-8 that the history for the back button shows page 1, which is where we were just before going to page 2. Also notice the three buttons, which are implemented in Example 1-18 to demonstrate navigating to a specific page, navigating backward, and navigating forward.

-----------------------------------------------------------------------------

单击page 1导航到page 2的超链接,如图1-8所示.

注意图1-8中后退按钮的历史下拉菜单显示page 1,这正是我们在page 2之前所在的页.顺便注意一下例1-18中声明的那三个用于导航的按钮,即确定的某一页,后退,以及前进.

 

Example 1-18. Custom navigation code // Page2.xaml.cs using System; using System.Windows; using System.Windows.Navigation; namespace MyNavApp { public partial class Page2 : Page { void page1Button_Click(object sender, RoutedEventArgs e) { NavigationService.GetNavigationService(this). Navigate(new Uri("page1.xaml", UriKind.Relative)); } void backButton_Click(object sender, RoutedEventArgs e) { NavigationService.GetNavigationService(this).GoBack( ); } void forwardButton_Click(object sender, RoutedEventArgs e) { NavigationService.GetNavigationService(this).GoForward( ); } } }

Example 1-18 shows the use of static methods on the NavigationService class to navigate manually just as the hyperlink, back and forward buttons do automatically.

----------------------------------------------------------------

例1-18展示了使用NavigationService类中的静态方法手动导航以及自动后退或前进.

 

 

 

PS:

没有图啊,PDF图没办法抠下来.

原文使用的好像是WPF早期的测试版本,里面很多东西都不一样.本文涉及的东西好像大多都不太适用,因为根本就没有NavigationApplication这个类了-_-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值