Windows 8应用程序打印预览并不总是呈现

时间:2015-07-20 00:21:59

标签: c# xaml windows-8 windows-store-apps

我正在尝试为我的Windows 8应用设置打印,但出于某种原因,页面并不总是在预览中呈现。有时它们只是延迟,有时它们只会渲染一些页面,而有时候它只会超时并且没有预览。"没有预览。"我已经将代码缩减为最简单的示例,该示例仍然表现出每个页面上基本上只是一个红色框的行为。

PrintingTest.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="5*"/>
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Column="1">
            <TextBox Name="WebAddress" Text="http://slashdot.org" Grid.Row="0" Margin="10,40,10,10" Width="400"/>
            <Button Content="Go" Click="Button_Click" Margin="10,40,10,10"/>
            <Button Content="Print" Click="Button_Click_1" Margin="10,40,10,10"/>
        </StackPanel>
        <WebView x:Name="PrintingWebView" Grid.Column="1" Grid.Row="1" Width="800" Opacity="1" ScrollViewer.VerticalScrollBarVisibility="Visible" />
        <WebView Name="TheWebView" Grid.Row="1" Grid.Column="1"  LoadCompleted="TheWebView_LoadCompleted"/>

        <StackPanel x:Name="PrintingRoot" Grid.Row="1">
            <Rectangle Fill="Blue" Width="100" Height="100"/>
        </StackPanel>

    </Grid>

PrintingTest.xaml.cs

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.TheWebView.Source = new Uri(this.WebAddress.Text);

            PrintController.PrintingWebView = this.PrintingWebView;
            PrintController.PrintingPanel = this.PrintingRoot;
            PrintController.DocumentTitle = this.WebAddress.Text;

            PrintController.RegisterForPrinting();
        }

        // Go
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.TheWebView.Source = new Uri(this.WebAddress.Text);
        }

        // Print
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
        }


    }

PrintController.cs

public static void RegisterForPrinting()
        {
            //await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();

            // Create the PrintDocument.
            printDocument = new PrintDocument();

            // Save the DocumentSource.
            printDocumentSource = printDocument.DocumentSource;

            // Add an event handler which creates preview pages.
            printDocument.Paginate += Paginate;

            // Add an event handler which provides a specified preview page.
            printDocument.GetPreviewPage += GetPrintPreviewPage;

            // Add an event handler which provides all final print pages.
            printDocument.AddPages += AddPrintPages;

            // Create a PrintManager and add a handler for printing initialization.
            PrintManager printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested += PrintTaskRequested;
        }


        private static void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;
            printTask = e.Request.CreatePrintTask(DocumentTitle, sourceRequested =>
            {
                printTask.Options.DisplayedOptions.Clear();

                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += async (s, args) =>
                {
                    // Notify the user when the print operation fails.
                    if (args.Completion == PrintTaskCompletion.Failed)
                    {

                    }

                };

                sourceRequested.SetSource(printDocumentSource);
            });
        }

        private static void AddPrintPages(object sender, AddPagesEventArgs e)
        {
            PrintDocument printDoc = (PrintDocument)sender;

            PrintingPanel.Children.Clear();

            // Loop over all of the preview pages and add each one to  add each page to be printed
            for (int i = 0; i < pages.Count(); i++)
            {
                UIElement grid = pages[i];

                printDoc.AddPage(grid);
            }

            // Indicate that all of the print pages have been provided
            printDoc.AddPagesComplete();
        }

        private static void Paginate(object sender, PaginateEventArgs e)
        {
            pages = new List<UIElement>();

            for (int i = 0; i < 5; i++)
            {

                Grid grid = new Grid();
                Rectangle rectangle = new Rectangle() { Width = 100, Height = 100 };
                rectangle.Fill = new SolidColorBrush(Colors.Red);
                grid.Children.Add(rectangle);

                PrintingPanel.Children.Add(grid);
                PrintingPanel.InvalidateMeasure();
                PrintingPanel.UpdateLayout();

                pages.Add(grid);
            }
            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(5, PreviewPageCountType.Intermediate);
        }

        private static void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
        {
            PrintDocument printDoc = (PrintDocument)sender;

            printDoc.SetPreviewPage(e.PageNumber, pages[e.PageNumber - 1]);

        }

0 个答案:

没有答案