NativeScript:为什么警报不会在条形码扫描仪承诺中执行?

问题描述 投票:0回答:1

我正试图弄清楚为什么警报不触发。 console.log语句可以工作,并且ObservableArray更新,并且显示在ListView屏幕上。我正在使用Android上的nativeScript-barcodescanner插件从cli运行NativeScript 6.3.3。我在搞什么?

barcode-view-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);
      }
    );
  }
}
android promise nativescript alert barcode-scanner
1个回答
0
投票

将警报添加到addItem函数以验证警报是否正常后,就可以解决此问题。然后,我使用Chrome调试来实现承诺。我注意到摄像头窗口似乎保持打开状态,警报将被拒绝。回到警报的nativescript-barcodescanner演示,发现警报周围包裹了setTimeout。补充了一下,现在一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.