Windows 10 UWP HTML/Winjs应用程序从共享魅力启动时未接收到激活的事件

Windows 10 UWP HTML/Winjs application not receiving onactivated event when started from the Sharing charm

本文关键字:启动 事件 激活 HTML UWP Winjs 共享 应用程序 Windows      更新时间:2024-01-12

我目前正在使用HTML5/WinJS技术开发Windows 10 UWP。我希望该应用程序成为PDF的共享目标。我已经把这个包括在清单中了,

  <Extensions> 
    <uap:Extension Category="windows.shareTarget"> 
      <uap:ShareTarget Description="Test Share Target"> 
        <uap:SupportedFileTypes> 
          <uap:FileType>.pdf</uap:FileType> 
        </uap:SupportedFileTypes> 
      </uap:ShareTarget> 
    </uap:Extension> 
  </Extensions> 

我在index.js 中有这个

  var app = WinJS.Application; 
  var activation = Windows.ApplicationModel.Activation; 
  app.onactivated = function (args) { 
      console.log("app.onactivated"); 
      if (args.detail.kind === activation.ActivationKind.launch) { 
          console.log("launch"); 
      if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) { 
          // TODO: This application has been newly launched. Initialize your application here. 
          console.log("newly launched"); 
      } else if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) { 
          var shareOperation = args.detail.shareOperation; 
          console.log(JSON.stringify(shareOperation, null, 4)); 
          } else 
      { 
        // TODO: This application has been reactivated from suspension. 
        // Restore application state here. 
      } 
      args.setPromise(WinJS.UI.processAll().then(function() { 
        // TODO: Your code here. 
      })); 

当我从Visual Studio 2015/直接在windows手机上调试构建内部启动应用程序时,我会看到控制台日志记录的预期输出。我还可以选择一个PDF文件,并将其共享到我的应用程序,然后它就会启动——但我在VS2015中根本看不到任何控制台输出。我正在寻找console.log(JSON.stringfy(shareOperation,null,4))的输出;

如果在谷歌和微软的网站上查找特定的Windows 10 UWP样本,但找不到任何东西,我只找到了一些信息片段。

当我的应用程序从共享魅力启动时,似乎没有收到激活的事件。

我做错了什么?

我注意到,的if语句

(args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget)

当前包含在的if语句中

(args.detail.kind === activation.ActivationKind.launch)

因此,除非您的应用程序执行"启动"操作,否则不会执行"shareTarget"的if语句。

你应该像这样提取出来:

if (args.detail.kind === activation.ActivationKind.launch) {
        console.log("launch");
        if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
            // TODO: This application has been newly launched. Initialize your application here. 
            console.log("newly launched");
        }
} else if(args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) {
        var shareOperation = args.detail.shareOperation;
        console.log(JSON.stringify(shareOperation, null, 4));
}

这是我做的一个演示,你可以参考:分享目标样本