How to clone Silverlight visual tree structure

六月ゝ 毕业季﹏ 提交于 2019-12-11 07:32:30

问题


I have the same problem as the question stated in "Printing in Silverlight 4".
To get around the problem, I have tried to scale transform root of my visual tree before printing.

    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        var renderScale = 1.0D;
        if (LayoutRoot.ActualWidth > e.PrintableArea.Width)
            renderScale = e.PrintableArea.Width/LayoutRoot.ActualWidth;

        var scaleTransform = new ScaleTransform();
        scaleTransform.ScaleX *= renderScale;
        scaleTransform.ScaleY *= renderScale;

        e.PageVisual = LayoutRoot;
        e.PageVisual.RenderTransform = scaleTransform;
    }

Now above code correctly prints out with silverlight visuals fit on a piece of paper.

The problem now is that LayoutRoot itself is now scaled down on the screen.
The question is, is there a way for me to create a clone of LayoutRoot before applying scale transform?

My walk-around is to applying the scale tranformation again after printing but I'd like to know if there is a way to clone visual tree


回答1:


My goodness, thanks for the question. I had the same problem but tried to fiddle about with setting the dimensions of a container (that is already in the visual tree) to the printable area, which does not work, as another layout pass seems to be required. ScaleTransform does work here however instantly.

I'm fine with the "work around" by just doing a myContainer.ClearValue(FrameworkElement.RenderTransformProperty) in the EndPrint event. Trying to clone the visual tree will yield a plethora of other issues (I have lazy loading content etc).




回答2:


Check out this link for details on silverlight object clone.

also just another idea would using xamlreader/writer to read the xaml string and creating an in-memory copy of the visual tree work.

for ex

If your xaml has button called originalbutton, using the code below you will have a copy of the button in readerLoadButton

// Save the Button to a string.
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);


来源:https://stackoverflow.com/questions/3848740/how-to-clone-silverlight-visual-tree-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!