C#POS打印机API:can';找不到打印机
我正在尝试使用C#POS打印机API构建一个应用程序 我成功地运行了此处提供的示例应用程序:,现在我正在尝试将API集成到我自己的UWP应用程序中 这就是我到目前为止所做的:C#POS打印机API:can';找不到打印机,c#,printing,uwp,posprinter,C#,Printing,Uwp,Posprinter,我正在尝试使用C#POS打印机API构建一个应用程序 我成功地运行了此处提供的示例应用程序:,现在我正在尝试将API集成到我自己的UWP应用程序中 这就是我到目前为止所做的: public class PrinterManager { private PosPrinter printer = null; private ClaimedPosPrinter claimedPrinter = null; private bool printerClai
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;
});
}
}
class PrinterHelper
{
公共静态异步任务GetFirstDeviceAsync(字符串选择器,Func convertAsync)
T:在哪里上课
{
var completionSource=new TaskCompletionSource();
var pendingTasks=新列表();
DeviceWatcher watcher=DeviceInformation.CreateWatcher(选择器);
watcher.Added+=(设备watcher发送方,设备信息设备)=>
{
Func lambda=async(id)=>
{
T=等待转换异步(id);
如果(t!=null)
{
completionSource.TrySetResult(t);
}
};
Add(lambda(device.Id));
};
watcher.EnumerationCompleted+=异步(DeviceWatcher发送方,对象参数)=>
{
//等待我们在“添加的”事件处理程序中创建的所有任务完成。
等待任务。WhenAll(等待任务);
//如果没有任务能够生成设备,则将结果设置为“null”。
completionSource.TrySetResult(null);
};
watcher.Start();
//等待枚举完成或找到设备。
T result=await completionSource.Task;
watcher.Stop();
返回结果;
}
//默认情况下,使用所有连接类型。
公共静态异步任务GetFirstReceiptPrinterAsync(PosConnectionTypes connectionTypes=PosConnectionTypes.All)
{
返回等待GetFirstDeviceSync(PosPrinter.GetDeviceSelector(connectionTypes),
异步(id)=>
{
PosPrinter printer=等待PosPrinter.FromIdAsync(id);
if(printer!=null&&printer.Capabilities.Receipt.IsPrinterPresent)
{
返回打印机;
}
//处理不需要的打印机。
打印机?.Dispose();
返回null;
});
}
}
我调用
EnablePrinter()
,我的应用程序正常运行,但在printer=wait PrinterHelper.GetFirstReceiptPrinterAsync()之后,打印机仍保持null
代码>行被执行。结果表明,这个问题相当愚蠢:
我需要转到Package.appxmanifest-->功能并启用“服务点”。事实证明,这个问题相当愚蠢:
我需要转到Package.appxmanifest-->Capabilities并启用“服务点”。啊,“服务点”,而不是“服务点”,啊,“服务点”,而不是“服务点”,当你已经有了答案时,别忘了标记这个案例。这将有助于其他人搜索。您好@Eutherpy…您能告诉我您是如何运行此示例的吗??你有物理POS打印机吗?当你已经有答案时,别忘了标记这个案例。这将有助于其他人搜索。您好@Eutherpy…您能告诉我您是如何运行此示例的吗??你有实体POS打印机吗??