如何在照片选择器任务中调用MessageBox

发布于 2024-12-25 19:03:19 字数 1229 浏览 1 评论 0原文

我有一个代码,我在 WP7 中调用 photochooser,我想在图片超过 2Mb 时向用户显示一个消息框。当我尝试这样做时,由于照片选择器任务在后台运行,我们开始收到未处理的异常。

void photoChooserTask_Completed(object sender, PhotoResult e)
{
       if (e.ChosenPhoto != null)
       {
           ProgressBar.Visibility = Visibility.Visible;

           image = _UploadImgeViewModel.ReadToEnd(e.ChosenPhoto);
           if (image.Length < 16384)
           {
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                UserSession.ProfileImage = bi;
                Session.PreviousImage = bi;
                UserSession.isImageChanged = true;
                UserSession.image = image;
                UserSession.Uploadimage = image;
                NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
            }
            else
            {
                ProgressBar.Visibility = Visibility.Collapsed;
                UserSession.isImageChanged = false;
                UserSession.ProfileImage = null;


                Dispatcher.BeginInvoke(() => MessageBox.Show("The message")); 
            }
        }
}       

#endregion

这仅显示后台作业正在恢复...以及前台的消息框。几秒钟后,应用程序崩溃了。 你能帮我解决这个问题吗?

I have a code where I am calling photochooser in WP7 and I want to show a messagebox to user when the pic is more than 2Mb. When I try to do this, since the photochooser task is running in background, we start getting unhandled exceptions.

void photoChooserTask_Completed(object sender, PhotoResult e)
{
       if (e.ChosenPhoto != null)
       {
           ProgressBar.Visibility = Visibility.Visible;

           image = _UploadImgeViewModel.ReadToEnd(e.ChosenPhoto);
           if (image.Length < 16384)
           {
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                UserSession.ProfileImage = bi;
                Session.PreviousImage = bi;
                UserSession.isImageChanged = true;
                UserSession.image = image;
                UserSession.Uploadimage = image;
                NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
            }
            else
            {
                ProgressBar.Visibility = Visibility.Collapsed;
                UserSession.isImageChanged = false;
                UserSession.ProfileImage = null;


                Dispatcher.BeginInvoke(() => MessageBox.Show("The message")); 
            }
        }
}       

#endregion

This only shows the background job as resuming... and the msg box in foreground. and after a few seconds, the app crashes.
Can you please help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2025-01-01 19:03:19

凉爽的。我有一些想法来解决这个问题。可能不是解决办法,但这样我们就可以避免这个问题。只需添加一个按钮并在按钮单击事件中执行验证过程即可。因为当导航正在进行时我们无法显示消息框。

下面是代码:

void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.ChosenPhoto != null)
        {
            ProcessSelectedImage(e.ChosenPhoto);
        }
    }       
    private void ProcessSelectedImage(Stream stream)
    {
        if (stream != null)
        {
            bi.SetSource(stream);
            UserSession.ProfileImage = bi;
            UserSession.PreviousImage = bi;
            image = ConvertToImage.ReadToEnd(stream);
            UserSession.image = image;
            UserSession.Uploadimage = image;

        }
    }



    private void UploadImage_Click(object sender, RoutedEventArgs e)
    {
        if (image.Length < 16384)
        {
            UserSession.isImageChanged = true;
            UserSession.image = image;
            UserSession.Uploadimage = image;
            NavigationService.Navigate(new Uri("/Views/EditMyProfile.xaml", UriKind.Relative));

        }
        else
        {

            UserSession.isImageChanged = false;
            UserSession.ProfileImage = null;
            UserSession.IsChangingProfilePicture = true;
            MessageBox.Show(MessageContent.ImageUploadLengh);

        }
    }

谢谢
卡迈勒

Cool. I got some idea to resolve this. Might not be a fix, but this way we can avoid this issue. Just add a button and do the validating process in the button click event. Since we can't display the message box when the navigation is in progress.

Below is the code:

void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.ChosenPhoto != null)
        {
            ProcessSelectedImage(e.ChosenPhoto);
        }
    }       
    private void ProcessSelectedImage(Stream stream)
    {
        if (stream != null)
        {
            bi.SetSource(stream);
            UserSession.ProfileImage = bi;
            UserSession.PreviousImage = bi;
            image = ConvertToImage.ReadToEnd(stream);
            UserSession.image = image;
            UserSession.Uploadimage = image;

        }
    }



    private void UploadImage_Click(object sender, RoutedEventArgs e)
    {
        if (image.Length < 16384)
        {
            UserSession.isImageChanged = true;
            UserSession.image = image;
            UserSession.Uploadimage = image;
            NavigationService.Navigate(new Uri("/Views/EditMyProfile.xaml", UriKind.Relative));

        }
        else
        {

            UserSession.isImageChanged = false;
            UserSession.ProfileImage = null;
            UserSession.IsChangingProfilePicture = true;
            MessageBox.Show(MessageContent.ImageUploadLengh);

        }
    }

Thanks
Kamal

好听的两个字的网名 2025-01-01 19:03:19

您有 10 秒的时间完全返回前台,否则您的应用程序将被终止。如果您有一个可以在此处显示的消息框,那么您将无法通过认证(因为用户在 10 秒内无法单击任何内容)——您需要等待页面加载。

如果您需要显示 MessageBox,解决此问题的方法是设置一个布尔值,并在页面的 Loaded 事件中检查它。

void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.ChosenPhoto != null) { ProgressBar.Visibility = Visibility.Visible;

            image = _UploadImgeViewModel.ReadToEnd(e.ChosenPhoto);
            if (image.Length < 16384)
            {
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                UserSession.ProfileImage = bi;
                Session.PreviousImage = bi;
                UserSession.isImageChanged = true;
                UserSession.image = image;
                UserSession.Uploadimage = image;
                NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
            }
            else
            {
                ProgressBar.Visibility = Visibility.Collapsed;
                UserSession.isImageChanged = false;
                UserSession.ProfileImage = null;

//set flag
                UserSession.ImageTooBig = true;
            }
        }
    }       

    #endregion


MyPage()
{
//make sure you attach Loaded Event if not already
Loaded += (s,e) => 
{
    if (UserSession.ImageTooBig)
     {
       UserSession.ImageTooBig = false;
       MessageBox.Show("Sorry, the image exceeds 2 MB");
      }
};
}

You have 10 seconds to return to the foreground completely or your app will be killed. If you have a messagebox that can display here, you will fail certification (because user could not click anything for 10 seconds) -- you need to wait for the page to load.

A workaround for this if you need to show a MessageBox is to set a bool, and check it in the Page's Loaded event.

void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.ChosenPhoto != null) { ProgressBar.Visibility = Visibility.Visible;

            image = _UploadImgeViewModel.ReadToEnd(e.ChosenPhoto);
            if (image.Length < 16384)
            {
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                UserSession.ProfileImage = bi;
                Session.PreviousImage = bi;
                UserSession.isImageChanged = true;
                UserSession.image = image;
                UserSession.Uploadimage = image;
                NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
            }
            else
            {
                ProgressBar.Visibility = Visibility.Collapsed;
                UserSession.isImageChanged = false;
                UserSession.ProfileImage = null;

//set flag
                UserSession.ImageTooBig = true;
            }
        }
    }       

    #endregion


MyPage()
{
//make sure you attach Loaded Event if not already
Loaded += (s,e) => 
{
    if (UserSession.ImageTooBig)
     {
       UserSession.ImageTooBig = false;
       MessageBox.Show("Sorry, the image exceeds 2 MB");
      }
};
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文