ShareOperation.ReportCompleted() 导致在 UWP 中抛出异常
ShareOperation.ReportCompleted() results in exception being thrown in 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();
崩溃,显示错误消息
"There was no match for the specified key in the index."
我试着搜索这个错误,让我找到了 this 问题,但它似乎是一个在后来的版本中消失的问题,现在我正面临这个问题,你建议我如何处理它。
根据Receive data的Report sharing status部分,
As a result, you shouldn't call it unless your app is at a point where it can be dismissed by the user.
我猜异常的原因是报告操作需要用户的权限。如果直接调用 ShareTargetActivated
中的 shareOperation.ReportCompleted();
将跳过用户授权。好像是不允许的。
对于解决方法,您可以在 Button_Click
或 OnGotFocus
等函数中处理代码 shareOperation.ReportCompleted();
。以下代码示例可以解决您的问题。
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。
我正在尝试处理共享操作 代码:
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();
崩溃,显示错误消息
"There was no match for the specified key in the index."
我试着搜索这个错误,让我找到了 this 问题,但它似乎是一个在后来的版本中消失的问题,现在我正面临这个问题,你建议我如何处理它。
根据Receive data的Report sharing status部分,
As a result, you shouldn't call it unless your app is at a point where it can be dismissed by the user.
我猜异常的原因是报告操作需要用户的权限。如果直接调用 ShareTargetActivated
中的 shareOperation.ReportCompleted();
将跳过用户授权。好像是不允许的。
对于解决方法,您可以在 Button_Click
或 OnGotFocus
等函数中处理代码 shareOperation.ReportCompleted();
。以下代码示例可以解决您的问题。
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。