//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);
}
上面的代码没有释放任何东西。一旦对流文档进行分页并获取页面的视觉表示,则流文档的内容将由 Visual 引用。因此,Visual 和 flowdocument 的内容都不会被处理。上面这段代码在执行时连续出现内存不足。
有谁知道如何释放所有这些资源所持有的内存?
提前致谢。