Implementing a Share target in a Windows Phone Silverlight 8.1 app
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Setting up a Share target in a Windows Phone Silverlight 8.1 app is slightly different than setting up a Share target in a Windows Phone Store app. There are three key differences:
In a Silverlight 8.1 app, the Share activation is handled by the Launching event. The LaunchingEventArgs must be cast to a ShareLaunchingEventArgs to check whether the app was launched as a Share target. If it was, the ShareLaunchingEventArgs object will contain the ShareTargetActivatedEventArgs needed to complete the share.
private void Application_Launching(object sender, LaunchingEventArgs e) { var shareEventArgs = e as ShareLaunchingEventArgs; if(shareEventArgs != null) { this.ShareOperation = shareEventArgs.ShareTargetActivatedEventArgs.ShareOperation; } }A URI mapper must be created so the Share target invocations can be mapped to the correct Share target page. Otherwise, the target app will not know what page to open when launched.
public class ShareUriMapper : UriMapperBase { public override Uri MapUri(Uri uri) { if ((Application.Current as App).ShareOperation != null) { return new Uri(uri.ToString().Replace("MainPage", "ShareTargetPage"), UriKind.Relative); } return uri; } }The URI mapper must be initialized as part of the root frame of the Silverlight 8.1 app in the InitalizeApplication method.
RootFrame.UriMapper = new ShareUriMapper();
Silverlight 8.1 apps that declare themselves as a Share target should not call the ShareStatusTask, ShareLinkTask or ShareMediaTask APIs. Doing so may cause the app to appear as a potential target when using one of these APIs, which is not supported and will cause the app to crash.