Windows Phone 8.1 application will work on ShowShareUI ()

I am trying to add share functionality to my Windows Phone application. The code behaves in an unpredictable way. Sometimes this works, but basically it is not, and I could not get any details about what causes the crash. Can someone please go through the code below and let me know if I missed something? Thanks!

public ArticlePage() { this.InitializeComponent(); //.. RegisterForShare(); } private void RegisterForShare() { DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView(); dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareLinkHandler); } private void ShareLinkHandler(DataTransferManager sender, DataRequestedEventArgs e) { DataRequest request = e.Request; DataRequestDeferral defferal = request.GetDeferral(); request.Data.Properties.Title = this.article.Title; request.Data.Properties.Description = this.article.Summary; request.Data.SetWebLink(new Uri(this.article.UrlDomain)); defferal.Complete(); } private void ShareCommand_Click(object sender, RoutedEventArgs e) { DataTransferManager.ShowShareUI(); } 

UPDATE

The code always works while I am debugging a visual studio, but in most cases this is not the case. I did a release build, thinking that there might be some code in the debug build that is causing the problem, but that doesn't make any difference.

+5
source share
3 answers

Ok, I had the same problem. ShowShareUi actually pauses your application. If you try to pause your application, you will receive an error message. This is a serialization issue.

If you want to study the error, then during debugging, click life cycle events and pause the work, now you will be disabled in debugging mode.

suspend in debug mode

If you navigate between pages with a custom class, you will get an error. * My suggestion is that you convert to jsonstring and send and return it. *

+1
source

I also had this problem recently. The general user interface crashes when one of the important parameters is not set. In your case, I suspect that

 this.article.UrlDomain 

is a null or invalid Uri pattern. You must create an if statement around it and make sure you are dealing with a real Uri. To test your code, you need to insert hard-coded constants and run it again. If it does not crash, check your Title , Summary and UrlDomain in turn.

Other places to explore:

Try adding a handler to the OnNavigatedTo method and delete it when you leave the page

 protected override async void OnNavigatedTo(NavigationEventArgs e) { DataTransferManager.GetForCurrentView().DataRequested += SharePage_DataRequested; } protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); DataTransferManager.GetForCurrentView().DataRequested -= SharePage_DataRequested; } 

I also looked for my code and looked at the official samples again and did not find any doffers. Just to be sure - if I were you, I would separate all the unprotected lines in my code and bring it as close as possible to the official examples, and then continue it back to where it was from there, so I would comment on these two lines:

 void SharePage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) { DataRequest request = e.Request; //DataRequestDeferral defferal = request.GetDeferral(); request.Data.Properties.Title = this.article.Title; request.Data.Properties.Description = this.article.Summary; request.Data.SetWebLink(new Uri(this.article.UrlDomain)); //defferal.Complete(); } 
+4
source

I had a similar problem (crash on ShowShareUI). After the very lengthy research that I discovered, this is due to an unhandled exception in SaveFrameNavigationState (SuspensionManager class from template project). In my case, this was due to the fact that the SessionStateForFrame method failed to process a class that cannot be serialized. Verify that you are saving the page state on the SaveState page. This happens not only in ShowShareUI, but also in suspend mode.

+1
source

Source: https://habr.com/ru/post/1206401/


All Articles