Silverlight打印template control问题,即使HasMorePages为false也无法停止打印

coudoufu 2013-01-17 10:10:40
我通过template control实现了如下功能:从数据库加载N条数据(内容为html),分页并以A4纸张大小模拟Word那样的效果连续显示在页面,我的实现方法如下:
1、定义一个容器模版控件继承于ItemsControl,用来显示所有A4大小的页面:
<Style TargetType="layout:QuestionCollectionContainer">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="layout:QuestionCollectionContainer">
<ScrollViewer Background="LightGray" Height="auto">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


2、定义页控件继承于ItemsControl,用来模拟Word那样的A4大小页:
<Style TargetType="layout:QuestionPage">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="layout:QuestionPage">
<Grid Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Margin="0,15,0,0" Background="White" x:Name="PageGrid">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Margin="{TemplateBinding Margin}" x:Name="PageContentAreaGrid">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<barcode:Code39 Height="50" x:Name="TopBarCode" HumanText="{TemplateBinding TopBarCodeText}" ShowHumanText="True" Width="300" HorizontalAlignment="Left"></barcode:Code39>
<ItemsPresenter Grid.Row="1" />
<Canvas Grid.Row="2" Height="25" x:Name="PageFooterCanvas" VerticalAlignment="Bottom">
<TextBlock x:Name="PageNumberInforText">
<Run x:Name="PageNumberRun"></Run>
</TextBlock>
<barcode:Code39 x:Name="BottomBarCode" HumanText="{TemplateBinding BottomBarCodeText}" ShowHumanText="False" Width="300" Height="25"></barcode:Code39>
</Canvas>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

一页的所有html数据项,都加载到ItemsPresenter中,现在分页及html加载都没问题了,我像如下这样来打印:
private void btnprint_Click(object sender, RoutedEventArgs e)
{
CurrentPage = 0;

PrinterFallbackSettings settings = new PrinterFallbackSettings();
settings.ForceVector = true;
settings.OpacityThreshold = 0.5;

PrintDocument pd = new PrintDocument();
pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);
pd.Print("test", settings);
}


void pd_PrintPage(object sender, PrintPageEventArgs e)
{
if (CurrentPage < (this.questionCollectionContainer.Items.Count - 1))
{
e.HasMorePages = true;

this.CurrentPage++;
e.PageVisual = (QuestionPage)questionCollectionContainer.Items[CurrentPage - 1];
}
else
{
e.HasMorePages = false;
this.CurrentPage++;
e.PageVisual = (QuestionPage)questionCollectionContainer.Items[CurrentPage - 1];
}
}


发现即使所有页都打印完后HasMorePages为false的时候,pd_PrintPage仍然被调用很多次,结果导致questionCollectionContainer.Items超出了索引界限报错,于是我改动程序让索引超出界限的时候不进行任何打印直接return,但是这样pd_PrintPage空跑几次后,没打印任何内容出来。查看windows打印任务窗口,发现页码始终为N/A。同样的这段打印代码,我用来打印StackPanel的好几个A4大小Grid控件却没任何问题,但是一打印我自己做的控件,就失控了。这个该怎么去解决?
...全文
192 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jv9 2013-01-19
  • 打赏
  • 举报
回复
我认为打印代码应该没问题,而在预打印时对于template内容处理出现异常,打印内容是从template中载入,然后分为打印的多页面,打印api可能出现异常。楼主可以试着debug,看看在打印前questionCollectionContainer.Items.Count是否正确。
Architecture Net 2013-01-17
  • 打赏
  • 举报
回复
应该是你的代码逻辑哪里有错误,请仔细检查一下,如果不行就先试试只打印一页数据试试。
coudoufu 2013-01-17
  • 打赏
  • 举报
回复
没人关注啊
coudoufu 2013-01-17
  • 打赏
  • 举报
回复
引用 2 楼 bushiyundeyu 的回复:
应该是你的代码逻辑哪里有错误,请仔细检查一下,如果不行就先试试只打印一页数据试试。
打印就那么几行代码,逻辑完全没问题,打其他非模版控件,不论怎么打,都没问题,打模版控件就出问题了,只打印一页是可以的。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧