Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF C#POS打印机,从下拉菜单中打开设备_C#_Wpf_Printing_Pos_Pointofservice - Fatal编程技术网

WPF C#POS打印机,从下拉菜单中打开设备

WPF C#POS打印机,从下拉菜单中打开设备,c#,wpf,printing,pos,pointofservice,C#,Wpf,Printing,Pos,Pointofservice,我正试图用POS打印机打印一行简单的文本。目前,我没有连接实际的pos打印机,但我有microsoft pos打印机模拟器 目前我有一个combobox控件,它将列出所有找到的打印机。其想法是,在这个组合框中,我选择我想要的打印机,然后它将使用该打印机打印文本 获取所有打印机并将其添加到组合框的代码如下: // Get available printers and add them to the combobox in the settings menu PosExplorer

我正试图用POS打印机打印一行简单的文本。目前,我没有连接实际的pos打印机,但我有microsoft pos打印机模拟器

目前我有一个combobox控件,它将列出所有找到的打印机。其想法是,在这个组合框中,我选择我想要的打印机,然后它将使用该打印机打印文本

获取所有打印机并将其添加到组合框的代码如下:

// Get available printers and add them to the combobox in the settings menu
        PosExplorer posExplorer = new PosExplorer();
        DeviceCollection printers = posExplorer.GetDevices("PosPrinter");
       for (int i = 0; i < printers.Count; i++)
        {
            settings_ComboBox_Printer.Items.Add(printers[i].ServiceObjectName);
        }
我在button click事件的for循环中遇到的错误是打印机[i]返回的是DeviceInfo对象,而不是设备本身。所以我必须收回设备,然后认领并打开它。然而,我不知道如何获取实际设备并打开它


提前谢谢

我想出来了,你可以使用posexplorer类初始化打印机,方法是向它提供设备信息。如果其他人遇到此问题,我会更改代码,现在它对我有效:) 我还添加了一些检查,以确保打印机已准备好打印。 谢谢你的回复,节日快乐

private void ButCart_PrintOrder_Click(object sender, RoutedEventArgs e)
    {
        PosExplorer posExplorer = new PosExplorer();

        // Get connected printer devices
        DeviceCollection printers = posExplorer.GetDevices(DeviceType.PosPrinter);
        for (int i = 0; i < printers.Count; i++)
        {
            if (printers[i].ServiceObjectName == settings_ComboBox_Printer.Text)
            {
                selectedPrinter = posExplorer.CreateInstance(printers[i]) as PosPrinter;
            }
        }

        try
        {
            selectedPrinter.Open();
            if (!selectedPrinter.Claimed)
            {
                selectedPrinter.Claim(0);
                selectedPrinter.DeviceEnabled = true;
            }

            bool printerReady = true;
            if (selectedPrinter.CoverOpen)
            {
                MessageBox.Show("Printer cover is open.");
                printerReady = false;
            }
            if (selectedPrinter.PowerState == PowerState.OffOffline)
            {
                MessageBox.Show("Printer is has no power or is offline");
                printerReady = false;
            }
            if (selectedPrinter.State != ControlState.Idle)
            {
                MessageBox.Show("Printer is busy");
                printerReady = false;
            }

            if (printerReady)
            {
                string text = "test text";
                selectedPrinter.PrintNormal(PrinterStation.Receipt, text);
            }

        }
        catch (Exception ae)
        {
            MessageBox.Show("An error occured: " + ae.ToString());
        }
    }
private void ButCart\u print order\u Click(对象发送方,RoutedEventArgs e)
{
PosExplorer PosExplorer=新的PosExplorer();
//获取连接的打印机设备
DeviceCollection printers=posExplorer.GetDevices(DeviceType.PosPrinter);
对于(int i=0;i
using Microsoft.PointOfService;
PosPrinter selectedPrinter;
private void ButCart_PrintOrder_Click(object sender, RoutedEventArgs e)
    {
        PosExplorer posExplorer = new PosExplorer();

        // Get connected printer devices
        DeviceCollection printers = posExplorer.GetDevices(DeviceType.PosPrinter);
        for (int i = 0; i < printers.Count; i++)
        {
            if (printers[i].ServiceObjectName == settings_ComboBox_Printer.Text)
            {
                selectedPrinter = posExplorer.CreateInstance(printers[i]) as PosPrinter;
            }
        }

        try
        {
            selectedPrinter.Open();
            if (!selectedPrinter.Claimed)
            {
                selectedPrinter.Claim(0);
                selectedPrinter.DeviceEnabled = true;
            }

            bool printerReady = true;
            if (selectedPrinter.CoverOpen)
            {
                MessageBox.Show("Printer cover is open.");
                printerReady = false;
            }
            if (selectedPrinter.PowerState == PowerState.OffOffline)
            {
                MessageBox.Show("Printer is has no power or is offline");
                printerReady = false;
            }
            if (selectedPrinter.State != ControlState.Idle)
            {
                MessageBox.Show("Printer is busy");
                printerReady = false;
            }

            if (printerReady)
            {
                string text = "test text";
                selectedPrinter.PrintNormal(PrinterStation.Receipt, text);
            }

        }
        catch (Exception ae)
        {
            MessageBox.Show("An error occured: " + ae.ToString());
        }
    }