场景:引导模式锁定的应用程序接受一些用户输入(名称等)并将它们打印出来.用户无法选择打印机.
我计划的解决方案是保存打印机的URL,我有以下形式:
ipp://.local.:5001/
要从此存储的字符串构建UIPrinter对象:
var printerURL = NSUrl.FromString(SettingsService.OptPrinterUrl); var printer = UIPrinter.FromUrl(printerURL);
然后我打电话给:
printer.ContactPrinter((available) => { if (available) { // } });
要么
var printInterface = UIPrintInteractionController.SharedPrintController; printInterface.PrintToPrinter(printer, (handler, completed, error) => { if (!completed && error != null) { UIAlertView alert = new UIAlertView("Guest Check-In App", "Print Error", null, "Ok", null); alert.Show(); } });
没有积极的结果.
但是,当使用从UIPrinterPickerController返回的UIPrinter对象时(如下所示),它都可以工作.
var printerPicker = UIPrinterPickerController.FromPrinter(null); printerPicker.PresentFromRect(new CGRect(0,0,100,100),this.View,true, delegate(UIPrinterPickerController handler, bool completed, NSError error) { printer = printerPicker.SelectedPrinter; });
我甚至尝试从PrinterPicker获取UIPrinter对象,并尝试使用从PrinterPicker获取的UIPrinter对象中的url使用UIPrinter.FromUrl构建新的UIPrinter.
我只能创建一个工作的UIPrinter对象而不直接使用UIPrinterPickerController中的那个,如下所示:
var printerPicker = UIPrinterPickerController.FromPrinter(null); printerPicker.PresentFromRect(new CGRect(0,0,100,100),this.View,true, delegate(UIPrinterPickerController handler, bool completed, NSError error) { printerPickerPrinter = printerPicker.SelectedPrinter; }); var printer = (UIPrinter)UIPrinter.FromObject(printerPickerPrinter);
总结我需要的是一种"记住"打印机的方法,并使用该打印机在xamarin构建的iOS应用程序中自动打印.