windowswindows-phone-7crashaccess-violationpanorama-control

Changing background of a panorama control crashes the app with AccessViolationException in windows phone 7.5


I am currently working on a weather app in which i need to change the background of the panorama control whenever the weather condition changes and also when user tries to select other cities from the list with different weather conditions. The problem I am facing is it works fine sometimes but sometimes anyhow it generates AccessViolationException whenever I try to change the background of the panorama control. It generates even if it is in the try catch block. Kindly if anyone could help me resolve this. I am changing the background of the panorama control like this

        BitmapImage obj = Icons.getBackgroundImage(data.dailyWeatherData[0].weatherCode);
        brush.ImageSource = obj;//Icons.getBackgroundImage(data.dailyWeatherData[0].weatherCode);
        if(Panorama.Background != null)
            Panorama.Background = null;
        try
        {
            Panorama.Background = brush;
        }
        catch (AccessViolationException ex)
        {
            Panorama.Background = null;
        }
        finally
        {
            Panorama.Background = brush;
        }

In this code Icons.getBackgroundImage returns the BitmapImage which is a static variable in the Icons class. Kindly help me in solving this issue. I shall be thankful.


Solution

  • Put the UI updation code in Dispatcher as looks like other thread is also going on:

    Deployment.Current.Dispatcher.BeginInvoke(() =>
    Panorama.Background = brush;
    });