1

嘿伙计们,我一直在尝试使用 Jerry Nixon 提供的关于如何在 Windows Store App 中打印 WebView 内容的教程打印我的 Webview?以及来自 Adam Nathan 的“带有 xaml 和 c# 的 windows 8 应用程序”的教程,我从他的示例中附加了以下代码:

using System;
using Windows.Graphics.Printing;
using Windows.Graphics.Printing.OptionDetails;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Printing;

namespace Chapter19
{
public sealed partial class MainPage : Page
{
    // Supports printing pages, where each page is a UIElement
    PrintDocument doc = new PrintDocument();

    public MainPage()
    {
        InitializeComponent();

        // Attach handlers to relevant events
        doc.GetPreviewPage += OnGetPreviewPage;
        doc.AddPages += OnAddPages;

        PrintManager printManager = PrintManager.GetForCurrentView();
        printManager.PrintTaskRequested += OnPrintTaskRequested;
    }

    // Prepare the print preview pages
    void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        this.doc.SetPreviewPageCount(2, PreviewPageCountType.Final);
        if (e.PageNumber == 1)
        {
            this.doc.SetPreviewPage(1, new Viewbox
            {
                Child = new Button
                {
                    Content = "PAGE 1!",
                    Background = new SolidColorBrush(Colors.Red)
                }
            });
        }
        else
        {
            this.doc.SetPreviewPage(2, new Viewbox
            {
                Child = new Button
                {
                    Content = "PAGE 2!",
                    Background = new SolidColorBrush(Colors.Red)
                }
            });
        }
    }

    // Prepare the real pages
    void OnAddPages(object sender, AddPagesEventArgs e)
    {
        this.doc.AddPage(new Viewbox
        {
            Child = new Button
            {
                Content = "PAGE 1!",
                Background = new SolidColorBrush(Colors.Red)
            }
        });
        this.doc.AddPage(new Viewbox
        {
            Child = new Button
            {
                Content = "PAGE 2!",
                Background = new SolidColorBrush(Colors.Red)
            }
        });
        this.doc.AddPagesComplete();
    }

    // Prepare and perform the printing
    void OnPrintTaskRequested(PrintManager sender,
                              PrintTaskRequestedEventArgs args)
    {
        // This gets invoked as soon as the Devices pane is shown
        PrintTask task = args.Request.CreatePrintTask("Document Title",
          async (taskArgs) =>
          {
              // This is invoked on a background thread when the Print
              // button is clicked
              var deferral = taskArgs.GetDeferral();
              await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
              {
                  // This must run on the main thread
                  taskArgs.SetSource(doc.DocumentSource);
                  deferral.Complete();
              });
          });

        // Show custom options

        PrintTaskOptionDetails details =
        PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options);
        details.DisplayedOptions.Clear();
        details.DisplayedOptions.Add(StandardPrintTaskOptions.MediaSize);

        // A custom text option
        PrintCustomTextOptionDetails option1 = details.CreateTextOption(
          "CustomId1", "Header");
        details.DisplayedOptions.Add("CustomId1");

        // A custom list option
        PrintCustomItemListOptionDetails option2 = details.CreateItemListOption(
          "CustomId2", "Contents");
        option2.AddItem("customItemId1", "As Seen on Screen");
        option2.AddItem("customItemId2", "Summary View");
        option2.AddItem("customItemId3", "Full Details");
        option2.AddItem("customItemId4", "Multiple Columns");
        details.DisplayedOptions.Add("CustomId2");

        // Handle options changes
        details.OptionChanged += OnOptionChanged;
    }

    void OnOptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
    {
        // TODO: Handle custom options
    }
}
}

我遇到的问题是我正在努力将 jerry 生成的“MywebviewPages”放到 Nathans 示例中的“this.doc.SetPreviewPage”和“this.doc.AddPage”上。由于 winRT 上缺少 PDF api,我被迫使用 webview,因为它是获取表格的唯一方法。请帮忙

4

0 回答 0