c# - Xamarin 形成 UWP 静默打印
问题描述
我需要从 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 年左右)使用的代码似乎不再有效,所以我假设这已受到微软的限制。有谁知道实现我需要做的事情?
解决方案
您在完全信任组件的正确轨道上。静默打印是 UWP 上下文中的一个已知差距,目前需要完全信任组件。有一个UserVoice 条目允许探索直接打印。
您在使用完全信任功能时遇到的问题是您需要在清单中定义 rescap 前缀,如App 功能声明文档的 Restricted Capabilities 部分所示。
要声明受限功能,请修改您的应用程序包清单源文件 (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 中编写该组件。
推荐阅读
- python - 使用 boto3 过滤 s3 对象,使调用者不过滤
- javascript - wxPython中的网络浏览器可以使用javascript进行python函数调用吗?
- python - django-login-required 中间件给出类型错误
- angular - 使用异步管道显示从应用程序状态检索到的 Observable 数据
- php - PHP:递归地将parant子添加到多维数组
- javascript - REACTJS:如何知道 Promise 中的方法是否失败?
- rust - Rust 中的生命周期如何为函数工作?
- error-handling - 如何访问用户在命令中传递的内容以将其用作命令错误处理程序?不和谐.py
- angular - 如何在弹簧靴中使用角度束
- c - C 错误:int 之前的预期表达式(当我调用该函数时,我得到一个表达式丢失的错误)