为什么我的 Windows 应用商店应用在打印预览中没有显示任何内容? [英] Why is my Windows Store app not showing anything in the print preview?

查看:54
本文介绍了为什么我的 Windows 应用商店应用在打印预览中没有显示任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PrintSample 并成功将 BasePrintPage 代码修改为以下(只是试图将其缩小到可管理的大小和最简单的测试案例):

I was using the PrintSample and successfully modified the BasePrintPage code to the following (just trying to cut it down to a manageable size and the simplest case for testing):

        protected PrintDocument printDocument = null;
        protected IPrintDocumentSource printDocumentSource = null;
        internal List<UIElement> printPreviewElements = new List<UIElement>();
        protected event EventHandler pagesCreated;

        protected void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;
            printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
            {
                printTask.Completed += async (s, args) =>
                {
                    if (args.Completion == PrintTaskCompletion.Failed)
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                        {
                            MessageDialog dialog = new MessageDialog("Something went wrong while trying to print. Please try again.");
                            await dialog.ShowAsync();
                        });
                    }
                };
                sourceRequested.SetSource(printDocumentSource);
            });
        }

        protected void RegisterForPrinting()
        {
            printDocument = new PrintDocument();
            printDocumentSource = printDocument.DocumentSource;
            printDocument.Paginate += CreatePrintPreviewPages;
            printDocument.GetPreviewPage += GetPrintPreviewPage;
            printDocument.AddPages += AddPrintPages;
            PrintManager printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested += PrintTaskRequested;
        }

        protected void UnregisterForPrinting()
        {
            if (printDocument != null)
            {
                printDocument.Paginate -= CreatePrintPreviewPages;
                printDocument.GetPreviewPage -= GetPrintPreviewPage;
                printDocument.AddPages -= AddPrintPages;
                PrintManager printMan = PrintManager.GetForCurrentView();
                printMan.PrintTaskRequested -= PrintTaskRequested;
            }
        }

        protected void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            printPreviewElements.Clear();
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
            AddOnePrintPreviewPage(pageDescription);
            if (pagesCreated != null)
            {
                pagesCreated.Invoke(printPreviewElements, null);
            }
            ((PrintDocument)sender).SetPreviewPageCount(printPreviewElements.Count, PreviewPageCountType.Intermediate);
        }

        protected void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
        {
            ((PrintDocument)sender).SetPreviewPage(e.PageNumber, printPreviewElements[e.PageNumber - 1]);
        }

        protected void AddPrintPages(object sender, AddPagesEventArgs e)
        {
            foreach (UIElement element in printPreviewElements)
            {
                printDocument.AddPage(element);
            }
            ((PrintDocument)sender).AddPagesComplete();
        }

        protected void AddOnePrintPreviewPage(PrintPageDescription printPageDescription)
        {
            TextBlock block = new TextBlock();
            block.Text = "This is an example.";
            block.Width = printPageDescription.PageSize.Width;
            block.Height = printPageDescription.PageSize.Height;
            printPreviewElements.Add(block);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            RegisterForPrinting();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            UnregisterForPrinting();
        }

这在 PrintSample 中工作得很好而且很漂亮,显示带有 TextBox 文本这是一个示例"的打印预览页面.但是,当我将此代码放入我自己的应用程序中时,尝试从按钮调用打印会显示一个空白页面.那么我在这里错过了什么?我忘记做的 PrintSample 在做什么?

This works fine and dandy in the PrintSample, displaying a print preview page with the TextBox's text, "This is an example." But, when I throw this code into my own app, trying to invoke printing from a button displays a blank page. So what am I missing here? What is the PrintSample doing that I forgot to do?

推荐答案

哇,你们不会相信这个...我唯一要做的就是将请求的主题设置为 Light...否则,我猜它不知道给文本框提供什么样式......太荒谬了.你是认真的吗!?我不得不花几个小时尽可能多地破解打印样本......太糟糕了.

Wow, you guys won't believe this...the only thing I had to do was set the requested theme to Light...otherwise, I guess it has no clue what style to give the textboxes...so ridiculous. Are you serious!? I had to spend hours hacking as much out of the print sample as I could...so messed up.

无论如何,将 RequestedTheme="Light" 放在 App.xaml 中:

Anyways, put RequestedTheme="Light" in App.xaml:

<Application
    x:Class="BlahBlahBlah.AppBlah"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="BlahBlahBlah" RequestedTheme="Light">
</Application>

或者,您可以将 TextBlock 的前景属性设置为黑色的新 SolidColorBrush.

Alternatively you can get away with just setting the TextBlock's foreground property to a new SolidColorBrush of the color Black.

这篇关于为什么我的 Windows 应用商店应用在打印预览中没有显示任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆