Image Viewer in Single Item Mode

ImageViewer supports any number of items with multiple view layouts. However, some applications simply want to use the viewer with a single image and do not want to deal with setting up the layouts or dealing with the item collection. For these types of applications, ImageViewer offers the single item mode.

As described in Image Viewer Items, if there are items in the viewer, one of them is donated as the active item and from Image Viewer Layouts, the viewer supports the ImageViewerSingleViewLayout that displays a single item at the item - the active item. Therefore it is easy to setup the viewer to display a single item all the time:

In addition to using the ActiveItem property, the viewer also contains shortcut methods and properties that can be used in single item mode. The following is the same code as above using these shortcuts. Set an image into the viewer:

With these properties/methods, you can use the ImageViewer control without touching any of the "view" or "item" property or methods. These shortcuts perform all the necessary check to make sure an active item is added to the viewer. For example, ImageViewer.Image get method is implemented like this:

if (this.ActiveItem != null) // Do we have an active item?returnthis.ActiveItem.Image; // Yes, return its imageelsereturnnull; // No, return null

And the set method:

if (this.ActiveItem != null) // Do we have an active item?this.ActiveItem.Image = value; // Yes, set its image// Else, nothing to do

The following lists all the single item mode shortcuts and the equivalent internal code: