gpt4 book ai didi

c# - WPF 打印/xps 问题

转载 作者:行者123 更新时间:2023-11-30 21:01:43 25 4
gpt4 key购买 nike

我编写了以下代码块,当发送到物理打印机时可以完美地打印我的 ListBox,但是当尝试将它发送到 XPS 打印机驱动程序或使用 XpsDocumentWriter 类时(我假设它们在hood) 我收到以下异常:

System.ArgumentException 未被用户代码处理 Message=Width 和 Height 必须是非负数。 来源=ReachFramework 堆栈跟踪: 在 System.Windows.Xps.Serialization.VisualSerializer.WriteTileBrush(字符串元素、TileBrush 画笔、矩形边界)

异常显然指向一个没有正确宽度/高度的项目,但是我在将它发送到不同打印机(物理和 XPS 驱动程序)时调试了代码,但我没有发现任何差异。

下面是我如何创建视觉效果以发送到打印机:

private ScrollViewer GeneratePrintableView()
{
ScrollViewer scrollView = new ScrollViewer();

Grid grid = new Grid { Background = Brushes.White, Width = this.myListBox.ActualWidth, Height = this.myListBox.ActualHeight };

grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions[0].Height = new GridLength(0, GridUnitType.Auto);
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Auto);

// Add the title and icon to the top
VisualBrush titleClone = new VisualBrush(this.TitleBar);
var titleRectangle = new Rectangle { Fill = titleClone, Width = this.TitleBar.ActualWidth, Height = this.TitleBar.ActualHeight };
grid.Children.Add(titleRectangle);
Grid.SetRow(titleRectangle, 0);

this.myListBox.Width = this.myListBox.ActualWidth;
this.myListBox.Height = this.myListBox.ActualHeight;

VisualBrush clone = new VisualBrush(this.myListBox) { Stretch = Stretch.None, AutoLayoutContent = true };
var rectangle = new Rectangle { Fill = clone, Width = this.myListBox.ActualWidth, Height = this.myListBox.ActualHeight };
Border border = new Border { Background = Brushes.White, Width = this.myListBox.ActualWidth, Height = this.myListBox.ActualHeight };
border.Child = rectangle;
grid.Children.Add(border);
Grid.SetRow(border, 1);

scrollView.Width = this.myListBox.ActualWidth;
scrollView.Height = this.myListBox.ActualHeight;
scrollView.Content = grid;
scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;

return scrollView;
}

这是我的 DocumentPaginator 实现中的 GetPage 覆盖:

public override DocumentPage GetPage(int pageNumber)
{
Page page = new Page();
double z = 0.0;

this.grid = new Grid();
this.grid.RowDefinitions.Add(new RowDefinition());
this.grid.RowDefinitions[0].Height = new GridLength(0, GridUnitType.Auto);

this.grid.Children.Add(this.printViewer);
Grid.SetRow(this.printViewer, 0);

//Adjusting the vertical scroll offset depending on the page number
if (pageNumber + 1 == 1) //if First Page
{
this.printViewer.ScrollToVerticalOffset(0);
this.printViewer.UpdateLayout();
}
else if (pageNumber + 1 == _verticalPageCount) //if Last Page
{
if (this.printViewer.ScrollableHeight == 0) //If printing only single page and the contents fits only on one page
{
this.printViewer.ScrollToVerticalOffset(0);
this.printViewer.UpdateLayout();

}
else if (this.printViewer.ScrollableHeight <= this.printViewer.Height) //If scrollconentheight is less or equal than scrollheight
{
this.printViewer.Height = this.printViewer.ScrollableHeight;
this.printViewer.ScrollToEnd();
this.printViewer.UpdateLayout();
}
else //if the scrollcontentheight is greater than scrollheight then set the scrollviewer height to be the remainder between scrollcontentheight and scrollheight
{
this.printViewer.Height = (this.printViewer.ScrollableHeight % this.printViewer.Height) + 5;
this.printViewer.ScrollToEnd();
this.printViewer.UpdateLayout();
}
}
else //Other Pages
{
z = z + this.printViewer.Height;
this.printViewer.ScrollToVerticalOffset(z);
this.printViewer.UpdateLayout();
}

page.Content = this.grid; //put the grid into the page
page.Arrange(new Rect(this.originalMargin.Left, this.originalMargin.Top, this.ContentSize.Width, this.ContentSize.Height));
page.UpdateLayout();

return new DocumentPage(page);
}

有趣的是,如果我将矩形的填充更改为画笔而不是克隆,那么我不会收到异常并且输出的文件大小正确。

我花了一天多的时间试图调试为什么这不起作用,我希望外面有人看到过类似的问题或者能够指出我犯的任何错误。

感谢您的任何回复。

最佳答案

我不得不放弃寻找 VisualBrush 的解决方案。如果画笔的 Visual 中有一个 GroupBox,我永远无法让它生成 XPS 文件。它总是以

失败

System.ArgumentException 未被用户代码处理 Message=Width and Height 必须是非负数。 Source=ReachFramework StackTrace:在 System.Windows.Xps.Serialization.VisualSerializer.WriteTileBrush(字符串元素,TileBrush 画笔,矩形边界)

解决方法是克隆应该放在 VisualBrush (Is there an easy/built-in way to get an exact copy (clone) of a XAML element?) 中的内容并直接在 Grid 而不是 VisualBrush 中使用它

关于c# - WPF 打印/xps 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13930045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com