未处理的异常引发的PhotoChooserTask异常、未处理、PhotoChooserTask

2023-09-04 11:03:18 作者:宇宙猛男、

我这有code,我用它来显示一个按钮,允许用户从他的库中选择图像,并用它作为背景,我的应用程序。

I've got this code and I'm using it to show a button which allows the user to choose an image from his library and use it as a background for my app.

所以,我创建了一个 PhotoChooserTask ,将其设置为显示摄像机,并将其绑定到了当任务完成后要执行一个方法。 该按钮将显示 PhotoChooserTask 启动任务。 做对完成的动作是很容易的,我刚刚拿到了设置一个布尔值,并更新图像源。

So I create a PhotoChooserTask, set it to show the camera and bind it to a method that has to be executed when the task is completed. The button will start the task by showing the PhotoChooserTask. The action to do on complete is quite easy, I've just got to set a boolean value and update an 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;
        }
    }

现在的问题是,它不会显示 PhotoChooserTask ,但它会给我一个异常,带我去。

The problem is that it won't show the PhotoChooserTask but it will give me an exception, taking me to

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

App.xaml.cs

这看起来奇怪,因为我已经在同一个班有另外一个 PhotoChooserTask ,这一次效果还算不错。

This looks weird as I've got another PhotoChooserTask in the same class and this one works fine.

有什么不对呢?

VisualStudio中甚至不会告诉我有什么异常,所以没有办法看着办吧!

VisualStudio won't even tell me what's the exception and so there's no way to figure it out!

编辑:

我刚刚发现,当我打电话的异常被抛出

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

pct_edit.Show(); 

按钮的点击事件。

in the button's tap event.

推荐答案

您可以使用尝试来检查是什么问题

You can use try to check what is the problem

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);
    }
};

把brakepoint对消息,那么你可以检查里面的一切

Put brakepoint on Message, then you can check everything inside ex.