元素的布局测量覆盖不应返回NaN值作为其DesiredSize [英] Layout measurement override of element should not return NaN values as its DesiredSize

查看:2876
本文介绍了元素的布局测量覆盖不应返回NaN值作为其DesiredSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了类似的问题,当我尝试调用测量和动态计算大小传递给方法:

I've run into a similar issue when I try to call Measure and pass the dynamically calculated size to that method:

元素System.Windows.Controls.StackPanel的布局测量覆盖不应返回NaN值作为其DesiredSize。

Layout measurement override of element 'System.Windows.Controls.StackPanel' should not return NaN values as its DesiredSize.

我想即时创建一个StackPanel中并打印。这是我的code:

I am trying to create a StackPanel on the fly and print it. Here's my code:

StackPanel printPanel = new StackPanel();

PrintableArea.Children.Remove(ChartBorder);
printPanel.Children.Add(ChartBorder);

//Get selected printer capabilities
System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);

//Get scale of the print wrt to screen of visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / printPanel.ActualWidth, capabilities.PageImageableArea.ExtentHeight / printPanel.ActualHeight);

//Transform the Visual to scale
printPanel.LayoutTransform = new ScaleTransform(scale, scale);

//Get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

//Update the layout of the visual to the printer page size.
printPanel.Measure(sz);
printPanel.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

dialog.PrintVisual(printPanel, "Test");

任何线索这可能是这个原因?而奇怪的是这种情况只与的StackPanel 。如果我试图创建一个简单的的TextBlock 用于测试目的,并尝试打印,一切正常,没有错误。我想知道有什么不同的时候,我们称之为测量的StackPanel

Any clue what could be the cause of this? And the strange thing is this happens only with StackPanel. If I try to create a simple TextBlock for testing purpose and try to print that, everything works fine with no errors. I was wondering what's different when we call Measure for StackPanel?

任何帮助解决这一问题将是真正伟大!

Any help to fix this issue would be really great!

推荐答案

您可能正在传递尺寸(double.NaN,double.NaN)的测量呼叫,这是不好的。您printPanel.ActualWidth /的ActualHeight应该是0.0,这将导致NaN的。这就是双重定义楠:

Your are probably passing in Size(double.NaN, double.NaN) to the Measure call, which is bad. Your printPanel.ActualWidth/ActualHeight should be 0.0, which results in NaN. This is how Double defines NaN:

   public const double NaN = (double) 0.0 / (double) 0.0;

所以,你会需要传递的大小(double.PositiveInfinity,double.PositiveInfinity)来获得所需的大小。当您安排printPanel时,ActualWidth的/的ActualHeight应该是有效的。

So you would need to pass in Size(double.PositiveInfinity, double.PositiveInfinity) to get the desired size. After you arrange the printPanel, the ActualWidth/ActualHeight should be valid.

这篇关于元素的布局测量覆盖不应返回NaN值作为其DesiredSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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