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

使用WCF类库中的图像生成XPS文档时出现问题,wcf,image,documentation-generation,xps,xpsdocument,Wcf,Image,Documentation Generation,Xps,Xpsdocument,我有一个Silverlight应用程序,它使用web服务来创建XPS文档。文档模板在WCF类库中创建为XAML控件 public void GenerateXPS() { Type typeofControl = Type.GetType(DOCUMENT_GENERATOR_NAMESPACE + "." + ControlTypeName, true); FrameworkElement control = (FrameworkElement)(Activator.Creat

我有一个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的字符串属性)不会显示为未加载。我怎样才能解决这个问题?谢谢

由于您的操作超出了WPF的预期用途,因此绑定基础架构可能需要一个推动

在设置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);

我在这篇文章中介绍了这一点以及其他与XPS相关的内容。

绑定基础设施可能需要推进,因为您的操作超出了WPF的预期用途

在设置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);

我在本文中介绍了这个和其他XPS相关的内容。

该URL有效吗?如果您尝试在浏览器中打开它,它会显示吗?该URL有效吗?如果您尝试在浏览器中打开它,它会显示吗?如果我有一个
UserControl
,在特定事件(例如
myItem.Loaded
)后正确呈现自己,则dispatcher hack无法工作!我如何告诉它等待事件触发?如果我有一个
UserControl
,它在特定事件(例如
myItem.Loaded
)后正确地呈现自己,那么dispatcher hack就不起作用了!我如何告诉它等待事件发生?