如何在photochooser任务中调用MessageBox

时间:2012-01-07 04:15:12

标签: windows-phone-7 messagebox dispatcher

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

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

这只显示后台作业恢复...和前台的msg框。几秒钟后,应用程序崩溃了。 你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

冷却。我有点想法解决这个问题。可能不是一个修复,但这样我们可以避免这个问题。只需添加一个按钮,然后在按钮单击事件中执行验证过程。由于我们无法在导航过程中显示消息框。

以下是代码:

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

        }
    }

由于 卡迈勒

答案 1 :(得分:0)

你有10秒钟完全返回前台或你的应用程序将被杀死。如果你有一个可以在这里显示的消息框,你将失败认证(因为用户无法点击任何内容10秒) - 你需要等待页面加载。

如果你需要显示一个MessageBox,解决方法是设置一个bool,并在Page的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");
      }
};
}