//Create a User Control with a TextBlock
TextBlock textBlock = new TextBlock();
textBlock.Text = "Helo";
UserControl userCOntrol = new UserControl();
userCOntrol.Content = textBlock;
//Add the UserControl to a Table
table.RowGroups[0].Rows[0].Cells[0].Blocks.Add(new Paragraph(new InlineUIContainer(userCOntrol as UIElement)));
//Add the table to a FlowDocument
FlowDocument flowDocument = new FlowDocument();
flowDocument.Blocks.Add(table);
//Fetch the Visual representation of the page.
DocumentPage page = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator.GetPage(0);
//Extract the Visual from the page and add to a Fixed Page
FixedPage fixedPage = new FixedPage();
ContainerVisual container = new ContainerVisual();
container.Children.Add(page.Visual);
DrawingCanvas canvas= new DrawingCanvas();
canvas.AddVisual(container);
fixedPage.Children.Add(canvas);
//now write the FixedPage to an XPS file
using (XpsDocument xpsDoc = new XpsDocument("c:\\x.xps", FileAccess.ReadWrite))
{
XpsDocumentWriter xpsDw = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsDw.Write(kj);
}
上面的代码没有发布任何内容。一旦flowdocument被分页并且获取了页面的可视化表示,则流式文档的内容将由Visual引用。所以Visual都没有被处理掉,也没有文件的内容。 上面的代码连续执行时会增加内存。
有谁知道如何释放所有这些资源所拥有的内存?
提前致谢。