【发布时间】:2018-12-27 01:40:55
【问题描述】:
我正在编写一个应用程序,该应用程序需要打印到 USB 收据打印机(因此 POSPrinter 不适用),而无需打开“打印对话框”窗口。我找到了一个使用包含的 exe 从 UWP 应用程序中打印的示例(由@Stefan Wick 提供给this question)。该程序的示例在我的开发笔记本电脑上运行。当我将相同的代码放入我的 UWP 应用程序时,我得到以下异常:
-- System.UnauthorizedAccessException: '访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))'
完整的异常详情如下:
System.UnauthorizedAccessException HResult=0x80070005 消息=访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED)) 源=System.Private.CoreLib 堆栈跟踪: 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 在
中的 POSClient.Views.SellPage.d__60.MoveNext()
此代码块出现异常:
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (imageFile != null)
ApplicationData.Current.LocalSettings.Values["FileToPrint"] = imageFile.Path;
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
}
当LaunchFullTrustProcessForCurrentAppAsync 行尝试运行时。
它永远不会到达 exe,但如果它有帮助,这里是相关的代码:
static void Main(string[] args)
{
AutoResetEvent resetEvent = new AutoResetEvent(false);
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("FileToPrint"))
{
Image img = Image.FromFile(ApplicationData.Current.LocalSettings.Values["FileToPrint"] as string);
PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler((sender, e) =>
{
img = ResizeImage(img, e.Graphics.VisibleClipBounds.Size);
e.Graphics.DrawImage(img, Point.Empty);
e.HasMorePages = false;
});
doc.EndPrint += new PrintEventHandler((sender, e) =>
{
resetEvent.Set();
});
doc.Print();
}
resetEvent.WaitOne();
}
我相信这个错误是在尝试访问文件时,但它可能是在尝试访问 exe 时?我已经对文件夹设置了权限,以允许每个人都有读/写权限(出于测试目的),但仍然得到相同的异常。
此示例代码将选定的图像打印到默认打印机。一旦我开始工作,我将进行必要的修改以打印收据。
我对 UWP 比较陌生,对此感到困惑(上周我一直在寻找相关的东西,但仍然找不到答案)。任何意见将不胜感激!
谢谢。
【问题讨论】: