Bug 22265 - DisplayActionSheet generates InvalidOperationException on Windows Phone
Summary: DisplayActionSheet generates InvalidOperationException on Windows Phone
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.2
Hardware: PC Windows
: Highest critical
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-08-21 12:31 UTC by Sergio Paganoni
Modified: 2014-09-25 20:15 UTC (History)
7 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Sergio Paganoni 2014-08-21 12:31:15 UTC
On Windows Phone, the DisplayActionSheet generates an InvalidOperationException when combined with other await-ed methods.

The code generating the exception is the following:

private async Task TakeImage(int id)
{
    var value = await DisplayActionSheet("Image", "Cancel", null, "Take Picture",
        "Select from Gallery");

    if (value.Equals("Take Picture"))
    {
        // Xamarin.Forms.Labs TakePicture
        await _imagesViewModel.TakePicture(id);
    }
    else if (value.Equals("Select from Gallery"))
    {
        await _imagesViewModel.SelectPicture(id);
    }
}

The exception is thrown after a short time (1/2 sec) that the Image Picker is displayed. 
What is even stranger, is that if I put a breakpoint at the second await:

   await _imagesViewModel.TakePicture(id);

The program works without exception. Its like that the time taken by the breakpoint is giving enough time to "clean" the task of the DisplayActionSheet.

Exception Message: "An attempt was made to transition a task to a final state when it had already completed."

Full stacktrace:
   at System.Threading.Tasks.TaskCompletionSource`1.SetResult(TResult result)
   at Xamarin.Forms.ActionSheetArguments.SetResult(String result)
   at Xamarin.Forms.Platform.WinPhone.Platform.<>c__DisplayClass19.<.ctor>b__10(Object o, DismissedEventArgs args)
   at Microsoft.Phone.Controls.CustomMessageBox.Dismiss(CustomMessageBoxResult source, Boolean useTransition)
   at Microsoft.Phone.Controls.CustomMessageBox.OnNavigating(Object sender, NavigatingCancelEventArgs e)
   at System.Windows.Navigation.NavigationService.RaiseNavigating(Uri uri, NavigationMode mode, Boolean isFragmentNavigationOnly, Boolean isCancelable, Boolean isNavigationInitiator, IPhoneApplicationPage existingContentPage)
   at System.Windows.Navigation.NavigationService.Journal_NavigatedExternally(Object sender, JournalEventArgs args)
   at System.Windows.Navigation.Journal.OnNavigatedExternally(String name, Uri uri, NavigationMode mode)
   at System.Windows.Navigation.Journal.NavigateAway(NavigationDirection direction)
   at Microsoft.Phone.TaskModel.Interop.ITask.NavigateAway.Invoke(NavigationDirection direction)
   at Microsoft.Phone.TaskModel.Interop.Task.FireOnNavigateAway(Int32 direction)
Comment 1 frosio 2014-09-17 02:54:14 UTC
I've got the same problem. I have a list of file; when i click on item a DisplayActionSheet appear and ask me if i want to open the file or send it by email. I use DependecyService to open the PDF file or send it through Android and Windows Phone.
Everything works fine on Android, while on Windows Phone if I open for example a pdf, then return to the app and try to send (or open) the same (or another) file, after selecting the action on DisplayActionSheet I have a debugger error. The file is opened (sent), but when I return, the app crashes (exit, or appear a white screen with "resuming. . . " 
 
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.Interop.ni.dll
Additional information: An attempt was made to transition a task to a final state when it had already completed.

If I remove the actionsheet and open the file at every item click it works fine.

var action = await DisplayActionSheet(fileName, "Cancel", null, new String[] { "Open", "Email"});
                    switch (action)
                    {
                        case "Open":
                            await DependencyService.Get<IOpenPDF>().Show(fileName);
                            break;
                        case "Email":
                            DependencyService.Get<IOpenPDF>().SendEmail(fileName);
                            break;

                        default:
                            break;
                    };

I also noticed that when the displayactionsheet appears, as soon as I select an action, the windows phone status bar at the top become black.
Comment 2 Sergio Paganoni 2014-09-23 07:36:06 UTC
I also tried with Xamarin.Mobile and is generating the same issue, hopefully this issue will be addressed in the next release?
Comment 3 Dan Ardelean 2014-09-25 10:19:22 UTC
Encountered the same problem. It should be given a higher priority because in certain scenarios the DisplaActionSheet is not usable
Comment 4 Eric Maupin 2014-09-25 20:15:43 UTC
Fixed for next update.