/// <summary> /// GetDeviceSelector method returns the string needed to identify a PosPrinter. This is passed to FindAllAsync method to get the list of devices currently available and we connect the first device. /// </summary> private async Task <bool> FindReceiptPrinter() { if (printer == null) { rootPage.NotifyUser("Finding printer", NotifyType.StatusMessage); DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(PosPrinter.GetDeviceSelector()); if (deviceCollection != null && deviceCollection.Count > 0) { DeviceInformation deviceInfo = deviceCollection[0]; printer = await PosPrinter.FromIdAsync(deviceInfo.Id); if (printer != null) { if (printer.Capabilities.Receipt.IsPrinterPresent) { rootPage.NotifyUser("Got Printer with Device Id : " + printer.DeviceId, NotifyType.StatusMessage); return(true); } } else { rootPage.NotifyUser("No Printer found", NotifyType.ErrorMessage); return(false); } } else { rootPage.NotifyUser("No devices returned by FindAllAsync.", NotifyType.ErrorMessage); return(false); } } return(true); }
async void ClaimSamePrinter_Click() { ClaimResultText.Text = ""; rootPage.NotifyUser("", NotifyType.StatusMessage); if (rootPage.Printer == null) { rootPage.NotifyUser("Use scenario 1 to find a printer first.", NotifyType.ErrorMessage); return; } using (var printer = await PosPrinter.FromIdAsync(rootPage.Printer.DeviceId)) { if (printer == null) { rootPage.NotifyUser("Cannot create same printer as scenario 1.", NotifyType.ErrorMessage); } using (var claimedPrinter = await printer.ClaimPrinterAsync()) { if (claimedPrinter != null) { ClaimResultText.Text = "Claimed the printer."; // This scenario doesn't do anything with the printer aside from claim it. // The "using" statement will dispose the claimed printer. } else { ClaimResultText.Text = "Did not claim the printer."; } } // The "using" statement will dispose the duplicate printer. } }
// By default, use all connections types. public static async Task <PosPrinter> GetFirstReceiptPrinterAsync(PosConnectionTypes connectionTypes = PosConnectionTypes.All) { return(await DeviceHelpers.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; })); }
async void FindPrinter_Click() { isBusy = true; UpdateButtons(); rootPage.NotifyUser("", NotifyType.StatusMessage); rootPage.ReleaseAllPrinters(); // Select a PosPrinter device using the Device Picker. DevicePicker devicePicker = new DevicePicker(); devicePicker.Filter.SupportedDeviceSelectors.Add(PosPrinter.GetDeviceSelector()); // Anchor the picker on the Find button. GeneralTransform ge = FindButton.TransformToVisual(Window.Current.Content as UIElement); Rect rect = ge.TransformBounds(new Rect(0, 0, FindButton.ActualWidth, FindButton.ActualHeight)); DeviceInformation deviceInfo = await devicePicker.PickSingleDeviceAsync(rect); rootPage.deviceInfo = deviceInfo; PosPrinter printer = null; if (deviceInfo != null) { printer = await PosPrinter.FromIdAsync(deviceInfo.Id); } if (printer != null && printer.Capabilities.Receipt.IsPrinterPresent) { rootPage.Printer = printer; rootPage.NotifyUser("Found receipt printer.", NotifyType.StatusMessage); } else { // Get rid of the printer we can't use. printer?.Dispose(); rootPage.NotifyUser("Please select a device whose printer is present.", NotifyType.ErrorMessage); } isBusy = false; UpdateButtons(); }
/// <summary> /// Find the first receipt printer and create two instances of it. /// </summary> /// <returns></returns> private async Task <bool> FindReceiptPrinterInstances() { if (printerInstance1 == null || printerInstance2 == null) { rootPage.NotifyUser("Finding printer", NotifyType.StatusMessage); printerInstance1 = await DeviceHelpers.GetFirstReceiptPrinterAsync(); if (printerInstance1 != null) { rootPage.NotifyUser("Got Printer Instance 1 with Device Id : " + printerInstance1.DeviceId, NotifyType.StatusMessage); // Create another instance of the same printer. if (printerInstance2 != null) { printerInstance2.Dispose(); printerInstance2 = null; } printerInstance2 = await PosPrinter.FromIdAsync(printerInstance1.DeviceId); if (printerInstance2 != null) { rootPage.NotifyUser("Got Printer Instance 2 with Device Id : " + printerInstance2.DeviceId, NotifyType.StatusMessage); return(true); } else { rootPage.NotifyUser("Couldn't create second instance of printer.", NotifyType.StatusMessage); } return(false); } else { rootPage.NotifyUser("No Printer found", NotifyType.ErrorMessage); return(false); } } return(true); }