使用 WCF 类库中的图像生成 XPS 文档时出现问题

发布于 2024-08-16 13:12:09 字数 1054 浏览 2 评论 0原文

我有一个 Silverlight 应用程序,它使用 Web 服务来创建 XPS 文档。文档模板被创建为 WCF 类库中的 XAML 控件。

public void GenerateXPS()
{
    Type typeofControl = Type.GetType(DOCUMENT_GENERATOR_NAMESPACE + "." + ControlTypeName, true);
    FrameworkElement control = (FrameworkElement)(Activator.CreateInstance(typeofControl));

    control.DataContext = DataContext;

    FixedDocument fixedDoc = new FixedDocument();
    PageContent pageContent = new PageContent();
    FixedPage fixedPage = new FixedPage();

    //Create first page of document
    fixedPage.Children.Add(control);
    ((IAddChild)pageContent).AddChild(fixedPage);
    fixedDoc.Pages.Add(pageContent);
    XpsDocument xpsd = new XpsDocument(OutputFilePath + "\\" + OutputFileName, FileAccess.ReadWrite);
    System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
    xw.Write(fixedDoc);
    xpsd.Close();

    SaveToDocumentRepository();
}

为了将实际数据绑定到我的文档模板,我设置了控件的 DataContext 属性。问题是,当我查看 XPS 时,图像(我将图像控件的源绑定到表示图像 URL 的字符串属性)不会显示,就像未加载一样。我该如何解决这个问题?谢谢!

I have a Silverlight application which uses a web service in order to create XPS documents. The document templates are created as XAML controls in a WCF class library.

public void GenerateXPS()
{
    Type typeofControl = Type.GetType(DOCUMENT_GENERATOR_NAMESPACE + "." + ControlTypeName, true);
    FrameworkElement control = (FrameworkElement)(Activator.CreateInstance(typeofControl));

    control.DataContext = DataContext;

    FixedDocument fixedDoc = new FixedDocument();
    PageContent pageContent = new PageContent();
    FixedPage fixedPage = new FixedPage();

    //Create first page of document
    fixedPage.Children.Add(control);
    ((IAddChild)pageContent).AddChild(fixedPage);
    fixedDoc.Pages.Add(pageContent);
    XpsDocument xpsd = new XpsDocument(OutputFilePath + "\\" + OutputFileName, FileAccess.ReadWrite);
    System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
    xw.Write(fixedDoc);
    xpsd.Close();

    SaveToDocumentRepository();
}

In order to bind the actual data to my document template I set the DataContext property of the control. The problem is that when I look at my XPS, the images (I bind the Source of my Image controls to a string property that represents the URL of my image) are not displayed as if they were not loaded. How can I solve this problem? Thanks!

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

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

发布评论

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

评论(1

丿*梦醉红颜 2024-08-23 13:12:09

绑定基础结构可能需要推动,因为您正在 WPF 的预期用途之外进行操作。

设置数据上下文后尝试添加以下代码:

control.DataContext = DataContext;

// we need to give the binding infrastructure a push as we
// are operating outside of the intended use of WPF
var dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.Invoke(
   DispatcherPriority.SystemIdle,
   new DispatcherOperationCallback(delegate { return null; }),
   null);

我在此 博客文章

The binding infrastructure probably needs a push along because you are operating outside the intended use of WPF.

Try adding the following code after setting the datacontext:

control.DataContext = DataContext;

// we need to give the binding infrastructure a push as we
// are operating outside of the intended use of WPF
var dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.Invoke(
   DispatcherPriority.SystemIdle,
   new DispatcherOperationCallback(delegate { return null; }),
   null);

I cover this and other XPS related stuff in this blog post.

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