当我尝试调用Measure
并将动态计算的大小传递给该方法时,我遇到了类似的问题:
元素'System.Windows.Controls.StackPanel'的布局测量覆盖不应该将NaN值作为DesiredSize返回。
我正在尝试动态创建StackPanel并打印它。这是我的代码:
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
用于测试目的并尝试打印它,一切正常,没有错误。当我们为Measure
致电StackPanel
时,我想知道有什么不同?
解决这个问题的任何帮助都会非常棒!
答案 0 :(得分:2)
您可能正在将尺寸(double.NaN,double.NaN)传递给Measure调用,这很糟糕。 printPanel.ActualWidth / ActualHeight应为0.0,这会导致NaN。这就是Double定义NaN的方式:
public const double NaN = (double) 0.0 / (double) 0.0;
所以你需要传入Size(double.PositiveInfinity,double.PositiveInfinity)来获得所需的大小。排列printPanel后,ActualWidth / ActualHeight应该有效。