uwp cant find the defualt or any printer that has been connected to my computer meanwhile other apps or i can do printing in via the printdocment class but I need a silent pring for this porpuse i have
PosPrinter printer = await PosPrinter.FromIdAsync(id);
ClaimedPosPrinter claimed = await printer.ClaimPrinterAsync();
ReceiptPrintJob job = claimed.Receipt.CreateJob();
job.PrintLine("Hello World");and I get the id here
string id = "";
string deviceSelector = "System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"";
var devices = await DeviceInformation.FindAllAsync(deviceSelector);
ListOfPrinters.Items.Clear();
// Display information about each printer
foreach (var device in devices)
{
if (device.Name == "4BARCODE 4B-2083A")
{
Debug.WriteLine($"Printer Name: {device.Name}");
Debug.WriteLine($"Id: {device.Id}");
Debug.WriteLine($"Kind: {device.Kind}");
Debug.WriteLine($"IsDefault: {device.IsDefault}");
Debug.WriteLine("---");
ListOfPrinters.Items.Add(device.Name);
id = device.Id;
}
}the second bunch finds the printer i target even I can put the printer name in a combo box i have shared the printer id to the FromIdAsync method but the ClaimedPosPrinter throws an exception which says
System.NullReferenceException: 'Object reference not set to an instance of an object.'
printer was null.
Note// I have tried to put the id as hardcoded many ways
Note// I have tried GetDefaultAsync but had no chance

Jithu ThomasRank #437 · 5.7K reputationPosted Apr 4, 2024, 2:42 AM EDT
idobtained fromDeviceInformationmatches the actual printer you intend to use.FromIdAsyncandClaimPrinterAsyncare asynchronous methods, handle potential delays or errors usingawaitand proper exception handling.printerDevicecapability in your app manifest.string id = ""; string deviceSelector = "System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\""; try { var devices = await DeviceInformation.FindAllAsync(deviceSelector); ListOfPrinters.Items.Clear(); // Display information about each printer foreach (var device in devices) { if (device.Name == "4BARCODE 4B-2083A") { Debug.WriteLine($"Printer Name: {device.Name}"); Debug.WriteLine($"Id: {device.Id}"); Debug.WriteLine($"Kind: {device.Kind}"); Debug.WriteLine($"IsDefault: {device.IsDefault}"); Debug.WriteLine("---"); ListOfPrinters.Items.Add(device.Name); id = device.Id; break; // Exit the loop once the target printer is found } } if (string.IsNullOrEmpty(id)) { // Handle case where the target printer wasn't found Debug.WriteLine("Target printer not found."); return; } // Asynchronous operation with proper handling PosPrinter printer = null; try { printer = await PosPrinter.FromIdAsync(id); ClaimedPosPrinter claimed = await printer.ClaimPrinterAsync(); ReceiptPrintJob job = claimed.Receipt.CreateJob(); job.PrintLine("Hello World"); // Handle printing success (optional) } catch (Exception ex) { // Handle potential exceptions during asynchronous operations Debug.WriteLine($"Error claiming printer: {ex.Message}"); } finally { // Release resources if necessary (check PosPrinter documentation) if (printer != null) { printer.Dispose(); } } } catch (Exception ex) { // Handle other potential errors during initial setup Debug.WriteLine($"Error finding printers: {ex.Message}"); }