【发布时间】: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