2010-01-06 63 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