如何 运行 `microsoftTeams.media.scanBarCode` API 加载应用程序

How to run `microsoftTeams.media.scanBarCode` API on load of the application

我在 MS Teams 中从我的扩展应用程序调用 URL。

URL 只是一个 angular 应用程序。

在申请开始时,我想要o 运行这个特定的API microsoftTeams.media.scanBarCode

我可以 运行 单击事件 API 但是当我 运行 宁 OnInit 时没有任何反应。

Angular代码:

    config: microsoftTeams.media.BarCodeConfig = {
        timeOutIntervalInSec: 30,
    };

    constructor() {}

    ngOnInit(): void {
        microsoftTeams.initialize();
        microsoftTeams.media.scanBarCode((error: microsoftTeams.SdkError, decodedText: string) => {
            if (error) {
                if (error.message) {
                    console.log(' ErrorCode: ' + error.errorCode + error.message);
                } else {
                    console.log(' ErrorCode: ' + error.errorCode);
                }
                this.message = error;
            } else if (decodedText) {
                microsoftTeams.tasks.submitTask(decodedText);
            }
        }, this.config);
    }

我在尝试使用您的代码时遇到了同样的问题。我的猜测是在调用 scanBarCode 之前没有初始化某些东西。 所以我确实尝试在 microsoftTeams.initialize(); 回调中添加代码。

microsoftTeams.initialize(() => {
  const config: microsoftTeams.media.BarCodeConfig = {
    timeOutIntervalInSec: 30
  };

  // Method that enables the user to scan different types of barcode, and returns the result as a string.  
  microsoftTeams.media.scanBarCode((error: microsoftTeams.SdkError, decodedText: string) => {
    // If there's any error, an alert shows the error message/code
    if (error) {
      if (error.message) {
        alert(" ErrorCode: " + error.errorCode + error.message);
      } else {
        alert(" ErrorCode: " + error.errorCode);
      }
    } else if (decodedText) {
      setBarCodeValue(decodedText);
    }
  }, config);
});

它对我有用。请检查这是否也解决了您的问题。