Select Page

The Image control used in XAML does not really seem like a big deal.  Place it in your XAML and bind the Source Property to your Data.  Might look something like this:

<Image Source={Binding MyPath} />

However, there’s a case I came across that had me scratching my head today for a significant period of time.  I have a use-case where the image file represented by “MyPath” changes visual content, but the filepath/name stays the same.  As my XAML is above, the visual content of the image will not update in my View unless I restart my app.  Instead, I have to do two things.  The first is in the XAML:

      <BitmapImage CreateOptions="IgnoreImageCache" UriSource={Binding MyPath} />

Instead of directly binding Image.Source on a single line, I have to go a bit deeper so that I can set the “CreateOptions” only found on “BitmapImage”.  That “IgnoreImageCache” setting means that the Microsoft Framework will not try to reuse a version of the Image that was cached upon initial load.  In most cases, just this change makes things work.  At the very least, the app does not need to be restarted; although, the user might need to navigated away from and back to the View in question to see the change.

To go that extra mile and help ensure that a user does not need to play with the navigation, wherever in my code that I’m setting my “MyPath” on my Model or ViewModel, I want to do something like this:

public string MyPath
   get { return _myPath; }
      _myPath = string.Empty;
      _myPath = value;
protected string _myPath = string.Empty;

And with that, we have now dealt with the funky way Microsoft deals with Images.  In my case, I was writing up a UWP app, but I think this information might apply to the earlier versions of XAML tech too.