Silverlight Tip of the Day #88 – How to handle image AG_E_NETWORK_ERROR errors
If you have ever worked with images while developing and running Silverlight app you have more than likely come across the AG_E_NETWORK_ERROR exception. This exception is thrown if an image was not able to load for whatever reason (I.e. it couldn’t be found, corrupt, etc.).
In order to prevent your users from seeing this error it’s important to monitor for the ImageFailed event for each image your try to load.
The following code demo’s how to accomplish this for an image.
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
LoadImage("http://www.mysite.com/grass.png");
}
private void LoadImage(string url)
{
Image img = new Image();
img.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
Uri imageUri = new Uri(url, UriKind.RelativeOrAbsolute);
ImageSource imgSource = new BitmapImage(imageUri);
img.SetValue(Image.SourceProperty, imgSource);
LayoutRoot.Children.Add(img);
}
void img_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
// Handle the error here
}
In the img_ImageFailed() event add the custom code you will need to handle this error.
Thank you,
--Mike Snow
Subscribe in a reader