适当的方式来强制装载一个WPF视觉 [英] Appropriate way to force loading of a WPF Visual

查看:136
本文介绍了适当的方式来强制装载一个WPF视觉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 System.Printing 的命名空间。我终于想通了,我用的是API的部分时,得到空白结果的原因是因为的 视觉 对象,我试图打印不加载/初始化。如果我通过把它们放在一个适当大小的窗口和调用显示视觉对象显示()在打印前,然后我得到预期的结果。

因此​​,我想出了一个解决方法是调用此方法为每个视觉

 公共静态无效ShowVisual(视觉视觉)
{
    窗口窗口=新窗口
        {
            内容=视觉,
            SizeToContent = SizeToContent.WidthAndHeight,
            可见= Visibility.Hidden
        };
    window.Show();
    window.close()的;
}
 


这似乎是一个黑客,尤其是因为用户简单地看到窗框平局。我想,一定有不同的方式,它应该做的事情。不过,我不露面,任何其他解决方案。使用的是隐藏窗口真的是应该在这里完成?

使用菜单项,截至 WPF描述 - ?得到的UIElement的大小,内存不起作用。我看着力WPF控件在内存的渲染,但我不是真的想渲染视觉上的位图,这似乎是那是什么。呼叫 ApplyTemplate在()图片,作为中的 WPF力构建可视树没有帮助。


编辑:这是使用 ShowVisual ,而不是从上面

解决方案

  ///<说明>
///此方法需要被调用,以便
//元件以正确的大小明显地打印。
///< /说明>
私有静态无效ArrangeElement(UIElement的元素)
{
    VAR箱=新Viewbox的{儿童为元素};

    box.Measure(新尺寸(double.PositiveInfinity,double.PositiveInfinity));
    box.Arrange(新的矩形(box.DesiredSize));
}
 

解决方案

要打印的需要被添加到在WPF中的可视化树,以使测量和安排的过程被称为上的所有元素的项目可视化树要显示/打印或以其他方式显示。

我没有这样做了一段时间,但你可能会发现,将这些物品在后台视口中,然后打印他们解决了这个问题。这应该解决的需要实际显示在屏幕上,从而用户看到他们同时也迫使测量/排列过程

I have been struggling with printing using the System.Printing namespace. I have finally figured out that the reason I was getting blank results when using portions of the API was because the Visual objects I was trying to print were not Loaded/Initialized. If I display the Visual objects by putting them in an appropriately-sized Windows and calling Show() prior to printing, I then get the expected results.

Thus, the workaround I came up with was to call this method for every Visual

public static void ShowVisual(Visual visual)
{
    Window window = new Window
        {
            Content = visual,
            SizeToContent = SizeToContent.WidthAndHeight,
            Visibility = Visibility.Hidden
        };
    window.Show();
    window.Close();
}


This seems like a hack, especially since the user briefly sees the Window-frame draw. I figure there must be a different way it is supposed to be done. However, I am not turning up any other solutions. Is using a hidden Window really what is supposed to be done here?

Using a MenuItem as described at WPF - Get size of UIElement in Memory? does not work. I looked at Force rendering of a WPF control in memory but I am not really wanting to render the Visual to a bitmap which seems to be what that is for. Calling ApplyTemplate() on the Image that as described in wpf force to build visual tree did not help.


EDIT: This is the solution that is used instead of ShowVisual from above

/// <remarks>
/// This method needs to be called in order for 
//  the element to print visibly at the correct size.
/// </remarks>
private static void ArrangeElement(UIElement element)
{
    var box = new Viewbox {Child = element};

    box.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    box.Arrange(new Rect(box.DesiredSize));
}

解决方案

The items you want to print need to be added to the visual tree in WPF in order for the Measure and Arrange processes to be called on all the elements in the visual tree you want to show / print or otherwise display.

I haven't done this for a while but you may find that adding these items to a ViewPort in the background and then printing them solves the issue. This should get around the need for actually displaying them on the screen and thus the user seeing them whilst also forcing the Measure / Arrange processes.

这篇关于适当的方式来强制装载一个WPF视觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆