Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on this answer here on <a href="https://stackoverflow.com/questions/3807018/print-documents-via-wpf-controls-and-convert-to-xps">printing documents via Wpf-controls and convert to XPS</a> I came up with the following extension method to create the visual tree. It works well within NUnit without STA-thread or anything.</p> <pre><code>/// &lt;summary&gt; /// Render a UIElement such that the visual tree is generated, /// without actually displaying the UIElement /// anywhere /// &lt;/summary&gt; public static void CreateVisualTree(this UIElement element) { var fixedDoc = new FixedDocument(); var pageContent = new PageContent(); var fixedPage = new FixedPage(); fixedPage.Children.Add(element); pageContent.ToMaybeOf&lt;IAddChild&gt;().Do(c =&gt; c.AddChild(fixedPage)); fixedDoc.Pages.Add(pageContent); var f = new XpsSerializerFactory(); var w = f.CreateSerializerWriter(new MemoryStream()); w.Write(fixedDoc); } </code></pre> <p>Please note that </p> <ul> <li>the other answer uses an API of the Reach-dll that does not look like the API I am seeing. I assume that there are differences between .NEt Framework versions 3.5 and 4.0</li> <li>the <code>ToMaybeOf</code> stuff basically means to treat <code>pageContent</code> as <code>IAddChild</code> and do an action on that interface</li> <li>this will <strong>not</strong> work with an element of type <strong>Window</strong> since the element is essentially added as a child to a Visual and Window will complain bitterly about this.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload