NativeScript:为什么警报不在 barcodescanner 承诺中执行?

NativeScript: Why doesn't the alert execute in the barcodescanner promise?

我正在尝试找出警报未触发的原因。 console.log 语句起作用并且 ObservableArray 更新并且显示在 ListView 屏幕上。我是 运行 来自 cli 的 NativeScript 6.3.3,在 Android 上带有 nativeScript-barcodescanner 插件。我搞砸了什么?

条形码视图-model.ts:

/* 
  15 Jan 2020

  Scan-View-Model is based upon demos found in nativescript-barcode scanner by
  Eddie Verbruggen found at https://github.com/EddyVerbruggen/nativescript-barcodescanner
*/ 

import { Page } from "tns-core-modules/ui/page";
import { Observable, fromObject, EventData } from "@nativescript/core/data/observable";
import { ObservableArray, ChangedData } from "@nativescript/core/data/observable-array";
import { ItemEventData, ListView } from "@nativescript/core/ui/list-view";
import { alert } from "@nativescript/core/ui/dialogs";
import { BarcodeScanner } from "nativescript-barcodescanner";

class Item {
  barcode: string;
  format: string;
  id: number;

  constructor(barcode: string, format: string) {
    this.barcode = barcode;
    this.format = format;
    this.id = new Date().getTime();
  }
}

export class ScanViewModel extends Observable {

  barcodeVersion = "Testing (r01 v20200115.2) " + new Date().getTime();

  items: ObservableArray<Item>
  newItem: string = '';

  public message: string;
  private barcodeScanner: BarcodeScanner;

  constructor() {
    super();
    this.items = new ObservableArray<Item>([
      new Item("1234A", "-Test-"),
      new Item("0987Z", "-Test-")
    ]);

    this.barcodeScanner = new BarcodeScanner();
  }

  addItem() {
    this.items.push(new Item(this.newItem, "*Manual*"));
    this.set('newItem','');
  }


  public scanBarcode() {
    this.barcodeScanner.scan({
      formats: "QR_CODE, EAN_13",
      cancelLabel: "EXIT. Also, try the volume buttons!", // iOS only, default 'Close'
      cancelLabelBackgroundColor: "#333333",              // iOS only, default '#000000' (black)
      message: "Use the volume buttons for extra light",  // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.'
      preferFrontCamera: false,     // Android only, default false
      showFlipCameraButton: true,   // default false
      showTorchButton: true,       // iOS only, default false
      torchOn: false,               // launch with the flashlight on (default false)
      resultDisplayDuration: 500,   // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
      orientation: 'portrait',     // Android only, default undefined (sensor-driven orientation), other options: portrait|landscape
      beepOnScan: true,             // Play or Suppress beep on scan (default true)
      openSettingsIfPermissionWasPreviouslyDenied: true, // On iOS you can send the user to the settings app if access was previously denied
      closeCallback: () => {
        console.log("Scanner closed @ " + new Date().getTime());
      }
    }).then((result) => {
        // Note that this Promise is never invoked when a 'continuousScanCallback' function is provided
        console.log("Text: " + result.text + " Format: " + result.format);
        this.items.push(new Item(result.text, result.format));
        console.log(this.items);

        alert({
          title: "Scan result",
          message: "Format: " + result.format + ",\nValue: " + result.text,
          okButtonText: "OK"
        });
      }, (errorMessage) => {
        console.log("No scan. " + errorMessage);
      }
    );
  }
}

在向 addItem 函数添加警报以验证警报是否正常工作后解决了这个问题。然后我使用 Chrome debug 逐步完成承诺。我注意到摄像头 window 似乎保持打开状态并且警报会被拒绝。回到 nativescript-barcodescanner 演示,警报是否有效,发现警报周围有一个 setTimeout。添加了那个,现在一切正常。