C# POS 打印机 API : can't find printer

标签 c# printing uwp posprinter

我正在尝试使用 C# POS 打印机 API 构建应用程序。

我成功运行了此处提供的示例应用程序:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/PosPrinter ,现在我正在尝试将 API 集成到我自己的 UWP 应用程序中。

这是我目前所拥有的:

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:

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;
            });
    }
}

我调用了 EnablePrinter(),我的应用程序运行无异常,但打印机在 printer = await PrinterHelper.GetFirstReceiptPrinterAsync(); 行被执行。

最佳答案

事实证明这个问题很愚蠢:

我需要转到 Package.appxmanifest --> Capabilities 并启用“服务点”。

关于C# POS 打印机 API : can't find printer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47556149/

相关文章:

python - Django管理命令动态一行输出

mvvm - 如何使用 MVVM 为社区工具包 RadialGauge 制作动画?

c# - UWP slider ;隐藏顶部显示的值

c# - 如何在没有SSIS包的情况下将数据从一个数据库移动到另一个数据库

c# - 从 C# 打印数据的好方法?

c# - 获取错误的日期时间格式

c# - 如何在 C# 中打印 html

c# - 如何获取 Windows 10 的内容?UWP/C# 中的通知?

c# - 接口(interface) <T> 其中 T : class

c# - 从 ASP.NET 5 Web API 返回文件