2017-11-29 15 views
1

C#POSプリンタAPIを使用してアプリケーションを構築しようとしています。C#POSプリンタAPI:プリンタが見つかりません

ここで提供されているサンプルアプリケーションを正常に実行しました:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/PosPrinter、私は自分のUWPアプリケーションにAPIを統合しようとしています。

これは私がこれまで持っているものです。

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();行の後nullまま実行される。

+1

"P *** of P ***"ではなく、 "Point of Service" –

答えて

0

それは問題は非常に愚かだったことが判明:

私はPackage.appxmanifestに行くために必要な - >機能と「サービスのポイント」を有効にしてください。

+0

回答済みの場合は、このマークを付けることを忘れないでください。検索時に他の人に役立ちます。 –

関連する問題