c# - 将 PrintDialog 的 PrintQueue 设置为联网打印机时出错 - C# WPF XAML
问题描述
我正在尝试设置 WPF PrintDialog 以使用用户在表单上设置的值来控制基本打印选项,而无需向它们显示对话框。
目标是让他们一次性设置打印选项,然后在一天中使用这些设置数百次,除非他们决定这样做,否则无需更改它们。
该代码适用于我机器上安装的所有打印机,而不是网络打印机。如果我从列表中选择联网打印机,则会收到以下错误 -
System.Printing.PrintQueueException: '填充 PrintQueue 对象的属性时发生异常。Win32 错误:打印机名称无效。
奇怪的是,如果我将其中一台网络打印机设置为默认打印机,并添加逻辑以通过LocalPrintServer.DefaultPrintQueue
传递给 a 进行无人值守打印PrintDialog
,则此代码有效。
我已经剖析了处理打印机名称的方式,但我似乎无法找出传递网络打印机的正确方法。
这是我当前的代码(我已经尝试了很多次迭代,结果相同,但我已经记不清了......哈哈)。
XAML-我正在使用已安装的打印机列表填充 XAML 组合框,如下所示 -
<Window x:Class="PrintDialogTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Printing="clr-namespace:System.Printing;assembly=System.Printing"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="450">
<Window.Resources>
<Printing:LocalPrintServer x:Key="localPrintServer1"/>
<ObjectDataProvider x:Key="printerCollection"
ObjectInstance="{StaticResource localPrintServer1}"
MethodName="GetPrintQueues">
<ObjectDataProvider.MethodParameters>
<x:ArrayExtension Type="{x:Type Printing:EnumeratedPrintQueueTypes}">
<Printing:EnumeratedPrintQueueTypes>Local</Printing:EnumeratedPrintQueueTypes>
<Printing:EnumeratedPrintQueueTypes>Connections</Printing:EnumeratedPrintQueueTypes>
</x:ArrayExtension>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>.............
<ComboBox x:Name="cboPrinters" HorizontalAlignment="Left" Height="34" Margin="53,414,0,0" VerticalAlignment="Top" Width="354" ItemsSource="{Binding Source={StaticResource printerCollection}}" DisplayMemberPath="Name" FontSize="14"/>
这工作正常,并为我提供了所有已安装的打印机。
如果我运行该应用程序,并选择两台联网打印机以外的任何打印机,则下面的代码(来自MainWindow.xaml.cs
打印按钮的单击事件)有效 -
// Create a PrintDialog
PrintDialog printDlg = new PrintDialog();
// Create a PrintQueue and PrintServer - assign the user selected printer to the PrintQueue
printDlg.PrintQueue = new PrintQueue(new PrintServer(), cboPrinters.Text);
如前所述,我尝试了多种方法来格式化传递给的名称值,但PrintQueue
均未成功。
我虽然这可能是修复,但它在使用时也会触发错误 -
PrintServerException - “...名称无效”,即使我可以从 Windows 访问路径
希望我已经提供了足够的信息,但如果没有,请告诉我,我会添加任何其他需要的信息。
任何帮助将不胜感激,并感谢您提前回复。
更新- 如果我用网络打印服务器的名称对实例化进行硬编码PrintServer
,则该代码也适用于网络打印机(需要注意) -
printDlg.PrintQueue = new PrintQueue(new PrintServer(@"\\NetworkPrintServerNameHere"), (string)cboPrinters.SelectedValue);
但很明显,这段代码对于现实世界的使用并不实用,因为我们PrintServer
的打印机列表中可以有多个名称。我需要找到一种方法来获取PrintServer
当前所选打印机的名称。
解决方案
因此,使用为组合框创建记录集的请求顶部的代码,我们可以PrintSever
在运行时以编程方式解析当前所选打印机所需值的所选记录,方法如下:
printDlg.PrintQueue = new PrintQueue(new PrintServer(((System.Printing.PrintQueue)cboPrinters.SelectedItem).HostingPrintServer.Name), (string)cboPrinters.SelectedValue);
我不再收到错误,并且可以打印到用户选择的任何本地和网络打印机。
推荐阅读
- javascript - 在字符串javascript中查找数组的第一个索引
- influxdb - 更正 influxdb 中新测量的错误 host_1 标记
- java - JUNIT test - Eclipse IDE is throwing an error
- reactjs - Handling state react native
- r - R: How can I make scatter points (geom_point) fade by decreasing in size in animation?
- python - 数据帧子集中的第一条记录
- php - PHP Uploading Zero Bytes To S3
- python - Encrypt your entire program and decrypt only in-memory
- python - Add features to keras time series generator for LSTM
- freemarker - 如何从 cmsfn.link 的输出中删除 .html 扩展名?