使用VisualBrush在一个固定的(流)文档中存储时间控制的快照

4
我需要及时拍摄控件的快照,并将它们存储在一个FixedDocument中。问题是VisualBrush有些“懒”,当我将其添加到文档后,它不会自动计算。最终创建文档时,所有页面都包含相同(最后)状态的Control。虽然VisualBrush无法冻结,但是否还有其他机会呢?我希望在一页上拥有更多的快照,因此逐页生成文档不是我的解决方案。另外,将VisualBrush转换为位图也不行(我想保留它的向量)。简而言之 - 我需要以某种方式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);
}
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;

我使用它,但速度非常慢。 - lindexi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接