TemplateStudio icon indicating copy to clipboard operation
TemplateStudio copied to clipboard

ShareTarget only shows splash screen

Open Arko109 opened this issue 5 years ago • 7 comments

When sharing to an app with ShareTarget feature, only the app's splash screen is displayed (not the ShareTarget page).

Here's how I navigate to it in App.xaml.cs (I use Prism):

protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    base.OnShareTargetActivated(args);
    await InitializeFrameAsync(args);
    await LaunchApplicationAsync("ShareTarget", args.ShareOperation);
}

Additionally, when closing the window, the app crashes.

Am I doing something wrong?

Arko109 avatar Nov 10 '19 22:11 Arko109

Hi.

Activation from ShareTarget opens the app as a new modal window, so you can add this code on App.xaml.cs to show the shared content in the modal window.

protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    base.OnShareTargetActivated(args);
    var rootFrame = await InitializeFrameAsync(args);
    rootFrame.Navigate(typeof(ShareTargetPage), args.ShareOperation);
    Window.Current.Content = rootFrame;
    Window.Current.Activate();
}

The app still crashes when you close the modal window. I think this is related with a Prism threading issue.

mvegaca avatar Nov 14 '19 10:11 mvegaca

Hi, thank you for your reply. The ShareTarget page is displayed now.

I already had a problem with an app crashing on close, it was because the navigation parameter needs to be serializable. Maybe this is the problem here too.

Arko109 avatar Nov 14 '19 10:11 Arko109

@Arko109, were you able to fix the param serialization issue?

sibille avatar Nov 26 '19 14:11 sibille

@sibille no, I tried to serialize the SharedOperation with Json.NET but it is impossible to deserialize it later, because it has no constructor.

In the previous case I mentioned, I was able to solve the problem like this:

In App.xaml.cs:

rootFrame.Navigate(typeof(ShareTargetPage), JsonConvert.SerializeObject(args.ShareOperation));

And in ShareTargetPage.xaml.cs:

await ViewModel.LoadAsync(JsonConvert.DeserializeObject<ShareOperation>(e.Parameter as string));

However, in this case I am getting this exception:

Newtonsoft.Json.JsonSerializationException: 'Unable to find a constructor to use for type Windows.ApplicationModel.DataTransfer.ShareTarget.ShareOperation. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Data', line 1, position 8.'

Arko109 avatar Nov 26 '19 14:11 Arko109

@Arko109, I don't think serialization is the problem, I tried to pass a simple string as argument and got the same exception. I've opened an issue on the prism github to see if we can get some help there.

sibille avatar Feb 19 '20 10:02 sibille

We've always promoted using value types as navigation parameter (and retrieve the object from your manager/service/…) to keep backstack small and work around serialization issues (which date back to .NET Native issues). But since @sibille tried with a string, it's probably something else. I'll do my best to dig up the code and have a look at it this weekend.

bartlannoeye avatar Feb 19 '20 21:02 bartlannoeye

@bartlannoeye, did you have the chance to look at this? @LucasHaines, I think we should hide this feature in 3.6 until we fix it.

sibille avatar Mar 31 '20 06:03 sibille