如何引用我的 FlowDocument

发布于 2024-10-02 11:57:24 字数 397 浏览 3 评论 0原文

我在 WPF 控件库中定义了 FlowDocument(添加新项...,FlowDocument - 是文件的根元素)。我打算在多个上下文中使用它,例如在用户控件或窗口中,在数据绑定和导出到 xps 的代码中引用,等等。但我不知道如何获取对实例的引用这份文件。它似乎没有在编译的程序集中创建对象。

更具体地说,这是我的问题

<MyUserControl ........ >
  <FlowDocumentScrollViewer>
    <!-- doesn't work --><namespaceRef:MyFlowDocument /> 
  <FlowDocumentScrollViewer>
</MyUserControl>

I've defined a FlowDocument in a WPF control library (Add New Item..., FlowDocument -- is the root element of the file). I intend this to be used in several contexts such as in a user control or on a window, referenced in code for data-binding and export to xps, etc. But I can't figure out how to get a reference to an instance of this document. It doesn't seem to create an object in the compiled assembly.

More specifically, this is my problem

<MyUserControl ........ >
  <FlowDocumentScrollViewer>
    <!-- doesn't work --><namespaceRef:MyFlowDocument /> 
  <FlowDocumentScrollViewer>
</MyUserControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2024-10-09 11:57:24

最简单的解决方案可能是将您的 FlowDocument 放入资源字典中,然后使用 x:Key 这样的

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FlowDocument x:Key="myFlowDocument" ..>
    </FlowDocument>
</ResourceDictionary>

<FlowDocumentScrollViewer Name="flowDocumentScrollViewer">
    <StaticResource ResourceKey="myFlowDocument"/>
</FlowDocumentScrollViewer>

否则您必须设置 FlowDocument 来构建操作嵌入式资源并将其隐藏在代码后面,如下所示

Stream flowDocumentStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DocumentNameSpace.FlowDocumentName.xaml");
FlowDocument flowDocument = (FlowDocument)XamlReader.Load(flowDocumentStream);
flowDocumentScrollViewer.Document = flowDocument;

Update
我认为如果您想研究它,可以使用 ObjectDataProvider 来加载 FlowDocument。不管怎样,ResourceDictionary 似乎是最简单的出路。

The easiest solution is probably to put your FlowDocument inside a Resource Dictionary and then use the x:Key like this

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FlowDocument x:Key="myFlowDocument" ..>
    </FlowDocument>
</ResourceDictionary>

<FlowDocumentScrollViewer Name="flowDocumentScrollViewer">
    <StaticResource ResourceKey="myFlowDocument"/>
</FlowDocumentScrollViewer>

Otherwise you'll have to set the FlowDocument to build action Embedded Resource and lode it in code behind with something like this

Stream flowDocumentStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DocumentNameSpace.FlowDocumentName.xaml");
FlowDocument flowDocument = (FlowDocument)XamlReader.Load(flowDocumentStream);
flowDocumentScrollViewer.Document = flowDocument;

Update
I think it might be possible to use an ObjectDataProvider to load the FlowDocument if you want to look into it. Anyway, a ResourceDictionary seems like the easy way out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文