【问题标题】:Xamarin forms UWP Silent printingXamarin 形成 UWP 静默打印
【发布时间】:2018-10-12 16:39:53
【问题描述】:

我需要从 UWP Xamarin 表单应用程序打印 html 文档,但不会向用户显示打印确认对话框。 我已经研究过使用完全信任,但这似乎已被删除。 设置

<Capabilities>
    <rescap:Capability Name="runFullTrust"/>
</Capabilities>

报告摘要无效。

我也尝试过使用服务进行打印,但没有成功。 使用 InternetExplorer 或 WebBrowser 格式化和打印在 Windows 窗体应用程序中工作正常,但在服务中运行将永远不会从导航到文档或从命令返回。

void PrintOnStaThread(string htmlPath)
{
    const short PRINT_WAITFORCOMPLETION = 2;
    const int OLECMDID_PRINT = 6;
    const int OLECMDEXECOPT_DONTPROMPTUSER = 2;
    using (var browser = new WebBrowser())
    {
        DebugLog.WriteLog("Control WebBrowser created");
        browser.Navigate(htmlPath);
        while (browser.ReadyState != WebBrowserReadyState.Complete)
        {
            DebugLog.WriteLog("DoEvents loop");
            Application.DoEvents();
        }
        DebugLog.WriteLog("DoEvents loop finished");
        dynamic ie = browser.ActiveXInstance;
        ie.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION);
        DebugLog.WriteLog("DoEvents loop finished end of method");
    }
}

public void Print(string htmlFilename)
{
    documentLoaded = false;
    documentPrinted = false;
    DebugLog.WriteLog("Pre new InternetExplorer()");
    InternetExplorer ie = new  InternetExplorerClass();
    ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
    ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

    object missing = Missing.Value;
    DebugLog.WriteLog("Pre navigate");
    ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
    while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
    {
        Thread.Sleep(100);
    }
    DebugLog.WriteLog("Doc loaded");
    ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
    DebugLog.WriteLog("Printing");
    while (!documentPrinted)
    {
        Thread.Sleep(100);
        DebugLog.WriteLog("Printing loop");
    }
    DebugLog.WriteLog("Printed");
    ie.DocumentComplete -= ie_DocumentComplete;
    ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
    ie.Quit();
}

我尝试了这些方法的许多不同变体,结果都相同。

实现人们过去(大约在 2016 年左右)使用的代码似乎不再有效,所以我假设这已受到 Microsoft 的限制。 有人知道我需要做什么吗?

【问题讨论】:

    标签: c# xamarin service printing uwp


    【解决方案1】:

    您在完全信任组件的正确轨道上。静默打印是 UWP 上下文中的一个已知差距,目前需要完全信任组件。正在探索UserVoice entry to allow direct printing

    您在使用完全信任功能时遇到的问题是您需要在清单中定义 rescap 前缀,如 Restricted Capabilities section of the App capability declarations document 所示。

    要声明受限功能,请修改您的应用程序包清单源文件 (Package.appxmanifest)。添加 xmlns:rescap XML 命名空间声明,并在声明受限功能时使用 rescap 前缀。例如,以下是声明 appCaptureSettings 能力的方法。

    <?xml version="1.0" encoding="utf-8"?>
    <Package
        ...
        xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
        IgnorableNamespaces="... rescap">
    ...
    <Capabilities>
        <rescap:Capability Name="appCaptureSettings"/>
    </Capabilities>
    </Package>
    

    我不怀疑您从 2016 年开始查看的代码在 UWP 上下文中也不起作用。我不知道您在从服务打印时具体遇到了什么:这样做比正确设置服务以能够打印以及确保您使用打印 API 听起来要复杂得多设计用于服务(我最好的猜测是您使用的 IE 控件不是)。从您的应用程序中的完全信任组件进行打印将更加容易和直接。如果这是您最熟悉的框架,您可以在 WinForms 中编写该组件。

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 2018-04-09
      • 2021-05-10
      • 2018-06-27
      • 2019-03-29
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多