1

XPS ドキュメントを作成するために Web サービスを使用する Silverlight アプリケーションがあります。ドキュメント テンプレートは、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 を表す文字列プロパティにバインド) が読み込まれていないかのように表示されないことです。どうすればこの問題を解決できますか? ありがとう!

4

1 に答える 1

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 関連の内容について説明します。

于 2010-02-16T12:41:06.693 に答える