Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone 8.1 App crashes on ShowShareUI()

I'm trying to add share functionality to my Windows Phone App. The code behaves in an unpredictable way. Sometimes it works, but mostly it doesn't and I haven't been able to get any details about what's causing the crash. Could someone please go through the code below and let me know if I've 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'm debugging from visual studio but pretty much never otherwise. I made a release build thinking there might be some code in the debug build which is causing the problem but that didn't make any difference.

like image 809
W.K.S Avatar asked Nov 07 '14 09:11

W.K.S


2 Answers

I also had that problem recently. The share UI crashes when one of the important parameters is not set. In your case I'd suspect that

this.article.UrlDomain 

is null or not a valid Uri pattern. You should build an if-clause around it and make sure that you're dealing with a real Uri. To test your code you should insert hardcoded constants and run it again. If it doesn't crash, check your Title, Summary and UrlDomain one by one.

Other places to investigate:

Try adding your handler in the OnNavigatedTo method and remove it when you're leaving 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 searched my code and looked at official samples again and did not find any defferals. Just to be sure - if I were you I'd strip all unnessecary lines in my code and get it as closest as possible to the official examples and then extend it back to where it was from there which is why I would comment out these two lines as well:

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();
}
like image 71
Fred Avatar answered Oct 05 '22 20:10

Fred


Okay, I had the same problem. ShowShareUi actually suspends your app. If you try suspending your app you would get the error. It is actually the serialization problem.

If you want to look into the error, then while debugging, press the lifecycle events and suspend, you will crash in debug mode now.

suspend in debug mode

If you are navigating between pages with a custom class you would get error. *My suggestion is that you would convert to jsonstring and send and get it back.*

like image 27
Mani Avatar answered Oct 05 '22 20:10

Mani