4

我需要及时拍摄 Control 的快照并将它们存储在一个 FixedDocument 中。问题是 VisualBrush 在某种程度上是“懒惰的”,并且不会通过将其添加到文档来评估自己。当我最终创建文档时,所有页面都包含相同(最后)状态的控制。虽然 VisualBrush 不能被冻结,但还有其他机会吗?我想在一页上有更多快照,因此逐页生成文档对我来说不是解决方案。Aswel 将 VisualBrush 转换为位图(我想将它保存在矢量中)。简而言之 -I need to somehow Freeze() VisualBrush

for(;;)
{
    FixedPage page = new FixedPage();
    ...
    Rectangle rec = new Rectangle();
    ...
    rec.Fill = vb;
    page.Children.Add(rec);
    PageContent content = new PageContent();
    ((IAddChild)content).AddChild(page);
    doc.Pages.Add(content);
}
4

1 回答 1

4

我使用了序列化:

string svb = XamlWriter.Save(vb.CloneCurrentValue());
// Replace all "Name" attributes (I don't need them already and deserialization would crash on them) with "Tag" - not best practice but it's fast :)
svb = svb.Replace("Name", "Tag");
rect.Fill((VisualBrush)XamlReader.Parse(svb));

编辑

更好的方法是将 Visual 保存为 XPS 文档,然后将 Visual 取回。(反)序列化在 SharedSizeGroups 和许多其他“类似引用”的东西上存在一些问题。

XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
control.InvalidateArrange();
UpdateLayout();
writer.Write(control);
Visual capture = doc.GetFixedDocumentSequence().DocumentPaginator.GetPage(0).Visual;
于 2011-11-08T07:00:20.610 回答