2010-01-06 28 views
1

我有一个Silverlight应用程序,它使用Web服务来创建XPS文档。文档模板是作为WCF类库中的XAML控件创建的。用WCF类库中的图像生成XPS文档的问题

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

+0

该URL是否有效?如果您尝试在浏览器中打开它,它会显示吗? –

+0

是的URL是有效的 –

回答

1

绑定基础结构可能需要推进,因为您在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相关的东西在这个blog post

+0

如果我有一个'UserControl'在特定事件(例如'myItem.Loaded')后正确呈现自己,调度程序破解不起作用!我怎么能告诉它等待事件发生? – l33t