Unhandled exception thrown by PhotoChooserTask

I have this code and I use it to show a button that allows the user to select an image from his library and use it as a background for my application.

So, I create PhotoChooserTask , set it to show the camera and bind it to the method that should be executed when the task is completed. The button will launch the task, showing PhotoChooserTask . The completion action is pretty simple, I just have to set a boolean and update the image source.

 PhotoChooserTask pct_edit = new PhotoChooserTask(); pct_edit.ShowCamera = true; pct_edit.Completed += pct_edit_Completed; Button changeImageButton = new Button { Content = "Change Image" }; changeImageButton.Tap += (s, e) => { pct_edit.Show(); }; void pct_edit_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { bi.SetSource(e.ChosenPhoto); IsRebuildNeeded = true; } } 

The problem is that it will not show PhotoChooserTask , but that will give me an exception, forcing me to

 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (Debugger.IsAttached) { Debugger.Break(); } } 

in App.xaml.cs

This looks weird as I have another PhotoChooserTask in the same class and this file works fine.

What is wrong with him?

VisualStudio won't even tell me what an exception is, and therefore there is no way to figure it out!

EDIT:

I just found out that an exception is thrown when I call

 pct_edit.Show(); 

in the button click event.

+4
source share
2 answers

You must define your selection as a field in your class. It is a requirement that you have a page for PhotoChooser. Then you subscribe to it in your constructor. This is indicated on MSDN here.

 class SomeClass { readonly PhotoChooserTask pct_edit = new PhotoChooserTask(); SomeClass() { pct_edit.ShowCamera = true; pct_edit .Completed += new EventHandler<PhotoResult>(pct_edit_Completed); } } 
+1
source

You can use try to check what the problem is.

 changeImageButton.Tap += (s, e) => { try { PhotoChooserTask pct_edit = new PhotoChooserTask(); pct_edit.ShowCamera = true; pct_edit.Completed += (s,e) => { if (e.TaskResult == TaskResult.OK) { var bi = new BitmapImage() // maybe you didn't initialize bi? bi.SetSource(e.ChosenPhoto); IsRebuildNeeded = true; } } pct_edit.Show(); } catch (Exception ex) { Message.Show(ex.Message); } }; 

Set brakepoint to Message , then you can check everything inside ex .

+1
source

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


All Articles