简体   繁体   English

C#POS打印机API:找不到打印机

[英]C# POS Printer API: can't find printer

I am trying to build an app using the C# POS printer API. 我正在尝试使用C#POS打印机API构建应用程序。

I successfully ran the sample app provided here: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/PosPrinter , and now I am trying to integrate the API into my own UWP application. 我成功运行了此处提供的示例应用程序: https : //github.com/Microsoft/Windows-universal-samples/tree/master/Samples/PosPrinter ,现在我正在尝试将API集成到我自己的UWP应用程序中。

This is what I have so far: 这是我到目前为止的内容:

public class PrinterManager
{
        private PosPrinter printer = null;
        private ClaimedPosPrinter claimedPrinter = null;
        private bool printerClaimed = false;

        public PrinterManager()
        {
        }

        public async void EnablePrinter()
        {
            //find printer
            printer = await PrinterHelper.GetFirstReceiptPrinterAsync();
            //claim printer
            printerClaimed = (claimedPrinter = await printer.ClaimPrinterAsync()) != null;
        }
    ...
}

printerHelper.cs: printerHelper.cs:

class PrinterHelper
{
    public static async Task<T> GetFirstDeviceAsync<T>(string selector, Func<string, Task<T>> convertAsync)
where T : class
    {
        var completionSource = new TaskCompletionSource<T>();
        var pendingTasks = new List<Task>();
        DeviceWatcher watcher = DeviceInformation.CreateWatcher(selector);

        watcher.Added += (DeviceWatcher sender, DeviceInformation device) =>
        {
            Func<string, Task> lambda = async (id) =>
            {
                T t = await convertAsync(id);
                if (t != null)
                {
                    completionSource.TrySetResult(t);
                }
            };
            pendingTasks.Add(lambda(device.Id));
        };

        watcher.EnumerationCompleted += async (DeviceWatcher sender, object args) =>
        {
            // Wait for completion of all the tasks we created in our "Added" event handler.
            await Task.WhenAll(pendingTasks);
            // This sets the result to "null" if no task was able to produce a device.
            completionSource.TrySetResult(null);
        };

        watcher.Start();

        // Wait for enumeration to complete or for a device to be found.
        T result = await completionSource.Task;

        watcher.Stop();

        return result;
    }

    // By default, use all connections types.
    public static async Task<PosPrinter> GetFirstReceiptPrinterAsync(PosConnectionTypes connectionTypes = PosConnectionTypes.All)
    {
        return await GetFirstDeviceAsync(PosPrinter.GetDeviceSelector(connectionTypes),
            async (id) =>
            {
                PosPrinter printer = await PosPrinter.FromIdAsync(id);
                if (printer != null && printer.Capabilities.Receipt.IsPrinterPresent)
                {
                    return printer;
                }
                // Dispose the unwanted printer.
                printer?.Dispose();
                return null;
            });
    }
}

I call EnablePrinter() , and my application runs without exceptions, but the printer remains null after the printer = await PrinterHelper.GetFirstReceiptPrinterAsync(); 我调用EnablePrinter() ,我的应用程序无例外运行,但是在printer = await PrinterHelper.GetFirstReceiptPrinterAsync();之后, printer = await PrinterHelper.GetFirstReceiptPrinterAsync();保持为null printer = await PrinterHelper.GetFirstReceiptPrinterAsync(); line is executed. 行被执行。

It turns out that the problem was quite stupid: 原来,这个问题很愚蠢:

I needed to go to Package.appxmanifest --> Capabilities and enable "Point of Service". 我需要转到Package.appxmanifest->功能并启用“服务点”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM