ShareOperation.ReportCompleted()导致在UWP中抛出异常

时间:2016-06-08 14:37:03

标签: c# windows-runtime uwp

我正在尝试处理共享操作 代码:

protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
     ShareOperation shareOperation = args.ShareOperation;
     Uri uriReceived = null;
     if (shareOperation.Data.Contains(StandardDataFormats.WebLink))
        uriReceived = await shareOperation.Data.GetWebLinkAsync();
     shareOperation.ReportCompleted();
}

它在shareOperation.ReportCompleted();崩溃,显示错误消息为

  

“索引中指定的键没有匹配。”

我尝试搜索此错误让我遇到this问题,但似乎这是一个问题随着以后的版本而消失,现在我正面临这个问题你怎么建议我处理它。

1 个答案:

答案 0 :(得分:2)

根据接收数据的报告共享状态部分,

  

因此,除非您的应用程序处于可以被用户解雇的位置,否则不应该调用它。

我想异常的原因是报告操作需要用户的权限。如果您直接拨打shareOperation.ReportCompleted();中的ShareTargetActivated,则会跳过用户的授权。好像不允许这样。 对于变通方法,您可以在shareOperation.ReportCompleted();Button_Click等函数中处理代码OnGotFocus。以下代码示例可以解决您的问题。

App.xaml.cs代码:

protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        rootFrame = new Frame();
        rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
        rootFrame.NavigationFailed += OnNavigationFailed;
        Window.Current.Content = rootFrame;
    }
    rootFrame.Navigate(typeof(MainPage), args.ShareOperation);
    Window.Current.Activate();           
}

MainPage.xaml.cs代码:

  ShareOperation shareOperation;
  protected override async void OnGotFocus(RoutedEventArgs e)
  {
      Uri uriReceived = null;
      if (shareOperation.Data.Contains(StandardDataFormats.WebLink))
          uriReceived = await shareOperation.Data.GetWebLinkAsync();
      this.shareOperation.ReportCompleted();
      base.OnGotFocus(e);
  }
  protected override async void OnNavigatedTo(NavigationEventArgs e)
  {
      this.shareOperation = (ShareOperation)e.Parameter;
  }

更多详情请参阅官方sharetarget sample

相关问题