Silverlight4 多页打印实现
silverlight4 中需要編碼來指示打印多頁,舉個例子來說明如何實現。?
XAML如下:
<ScrollViewer VerticalScrollBarVisibility="Auto" Margin="8" BorderThickness="0"><StackPanel Name="spQues"></StackPanel></ScrollViewer>?
StackPanel?"spQues"的子項通過代碼生成,并且子項長度超過1頁。
?
打印代碼如下:
PrintDocument _print = new PrintDocument();List<UIElement> uis = new List<UIElement>();_print.PrintPage += (s, e) =>{StackPanel sp = new StackPanel();while (spQues.Children.Count > 0){var ui = spQues.Children[0];spQues.Children.RemoveAt(0);sp.Children.Add(ui);uis.Add(ui);sp.Measure(new Size(e.PrintableArea.Width, double.PositiveInfinity));if (sp.DesiredSize.Height > e.PrintableArea.Height && sp.Children.Count > 1){sp.Children.Remove(ui);uis.Remove(ui);spQues.Children.Insert(0, ui);e.HasMorePages = true;break;}}e.PageVisual = sp;}; _print.EndPrint += (s, e) =>{if (spQues.Children.Count > 0){for (int i = uis.Count() - 1; i >= 0; i--){TextBlock ui = (TextBlock )uis.ElementAt(i); ((StackPanel)ui.Parent).Children.Remove(ui);spQues.Children.Insert(0, ui);}}else{foreach (TextBlock ui in uis){ ((StackPanel)ui.Parent).Children.Remove(ui);spQues.Children.Add(ui);}}uis.Clear();};?
實現的原理就是將StackPanel?"spQues"的子項添加到一個臨時的StackPanel里面,這個臨時的StackPanel一次只增加一頁能夠打印完得內容,如果還有子項未添加完,則指示打印下一頁。在打印完成后將這些子項又加回到原來的StackPanel里。
總結
以上是生活随笔為你收集整理的Silverlight4 多页打印实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 调用栈(call stack)
- 下一篇: 基于Silverlight4开发的相关工