首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

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

2013-07-01 
Silverlight打印template control问题,即使HasMorePages为false也无法停止打印我通过template control实现

Silverlight打印template control问题,即使HasMorePages为false也无法停止打印
我通过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控件却没任何问题,但是一打印我自己做的控件,就失控了。这个该怎么去解决? silverlight print control?template template?control
[解决办法]
应该是你的代码逻辑哪里有错误,请仔细检查一下,如果不行就先试试只打印一页数据试试。
[解决办法]
我认为打印代码应该没问题,而在预打印时对于template内容处理出现异常,打印内容是从template中载入,然后分为打印的多页面,打印api可能出现异常。楼主可以试着debug,看看在打印前questionCollectionContainer.Items.Count是否正确。

热点排行