silverlightsilverlight-5.0image-cachingrichtextblock

Images inside RichTextBlock get hidden randomly


I have a Silverlight 5 NavigationApplication with a RichTextBlock and TreeView. every time one of TreeView items clicked, i add some images into RichTextBlock using code-behind. the problem is that sometimes when TreeView's SelectedItem is changed, some of added images dont appear inside RichTextBlock. but when i change TreeView's SelectedItem several times, the image appear again! i thought that was because of images caching then tried with using no cache for images, but it's the same. my code for loading images at runtime is this:

var uri = new Uri(imageSource, UriKind.Relative);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmapImage.UriSource = uri;
bitmapImage.ImageFailed += image_ImageFailed;
bitmapImage.ImageOpened += image_ImageOpened;
var border = new Border() { Width = 500, Height = 400 };
var image = new Image()
{
    HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
    VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
    Source = bitmapImage 
};
border.Child = image;
paragraph.Inlines.Add(new InlineUIContainer()
{
    Child = border
});  

If i leave BitmapCreationOption to it's default value, every image just appears one time and if i click back that TreeViewItem again, no images will be appear at all!
I have tried creating images in different size but there was no change, and also i tested loading images from absolute Uri too, but that was not helpful.
I am going to think about a bug in RichTextBlock! what do you think guys?


Solution

  • I have found the solution, i changed the BitmapImage.CreateOptions like this:

    bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.DelayCreation;  
    

    and everything works just fine :)