问题描述
我有一个 WPF 页面用作输入表单,其中一侧包含多个控件,另一侧包含一个流文档阅读器.
I have a WPF page used as an input form which contains a number of controls on one side, and a flow document reader on the other.
我想将此文档阅读器的内容设置为加载表单时加载的流文档的特定部分(通过加载事件).
I want to set the content of this document reader to a specific part of a flow document which is loaded when the form is loaded, (via a loaded event).
我找到了一篇文章,使用片段解释了如何执行此操作,但显示的示例仅以 XAML 表示.
I have found an article explaining how to do this, using fragments, but the examples shown are only expressed in XAML.
在我的例子中,当用户将焦点放在一个控件上时,我需要更新流文档阅读器的文档属性(我已经连接了事件),因此我需要在后面的代码中而不是 XAML 中执行此操作.
In my case I need to update the document property of the flow document reader when the user gives focus to one the controls (I have wired up the events already) therefore I need to do this in the code behind rather than XAML.
我尝试将文档属性设置为:
I have tried setting the document property to:
Document#Control_Sport
其中 Document 是 XAML 流文档的名称,Control_Sport 是我需要导航到的片段的名称.
where Document is the name of XAML flow document and Control_Sport is the name of the fragment I need to navigate to.
然而这会产生错误,它不喜欢那里有井号.
However this gives an error, it doesn't like the hash sign being there.
我尝试在 MSDN 上查看,但仅查看 XAML.有没有办法通过代码来做到这一点?
I tried looking on MSDN but its XAML only. Is there a way I can do this through code?
任何帮助将不胜感激.
菲利克斯
链接到 MSDN 文章:http://msdn.microsoft.com/en-us/library/ms750478.aspx#FragmentNavigation
Link to MSDN article: http://msdn.microsoft.com/en-us/library/ms750478.aspx#FragmentNavigation
推荐答案
首先,在 Page 或 Window 对象中创建一个 Frame 对象.将 JournalOwnership
属性设置为 "OwnsJournal"
将为文档提供自己的导航栏(向前和向后箭头以及历史记录).您可能还需要添加额外的参数来确定文档中的框架大小和位置,但我没有将它们包含在我的示例中,因为我不知道您的应用需要什么:
First, create a Frame object inside your Page or Window object. Setting the JournalOwnership
property to "OwnsJournal"
will give the document its own navigation bar (forward and back arrows plus a history). You will probably need to add additional parameters to size and locate the frame within your document as well, but I didn't include them in my example since I don't know what your app requires:
<Frame Name="MyFrame" JournalOwnership="OwnsJournal" />
然后,为您的文档片段创建一个包 URI.假定此文档与应用程序的可执行文件位于同一目录中;您需要在路径中添加更多内容以导航到文档在您的项目中所在的目录:
Then, create a pack URI for your document fragment. This document is assumed to be in the same directory as the application's executable; you will need to add more to the path to navigate to the directory where the document resides in your project:
Uri MyUri = new Uri("pack://application:,,,/MyXamlDocument.xaml#MyFragment");
然后,从按钮的 Click 处理程序内部导航到它,或者您喜欢启动导航的任何其他方式:
Then, navigate to it from inside your button's Click handler or whatever other means you like to initiate the navigation:
MyFrame.Navigate(MyUri);
这篇关于从后面的代码导航到流文档中的特定片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!