How to load file resources (XAML)
Learn to load file resources from various locations.
You can access file resources in app files that you deliver as part of the app package, or that you include as part of a component or framework package, from app data, or from the web. You can reference these files in markup (XAML attributes, or Notifications XML), or via code (such as Windows.Web.Http or storage file APIs). The examples here show the file resources in specific contexts, but you can use most of them in various other contexts.
To access files from the web in XAML, you can use a standard absolute HTTP URIs.
To access files from your application package, you can use either a direct or a logical file path to refer to the resource. This is true even if the files have multiple language, scale, contrast, or other variations. See Quickstart: Using file or image resources for an introduction.
For example, you load
by referring to
To access files relative to the current XAML page, you can use a relative URIs.
To access files relative to the root of the package, from a XAML page, you can use an absolute path URIs (those that begin with a "/").
<Image Source="/images/logo.png" />
To access files stored inside the application package, but from code where there is no inferred root authority, specify the ms-appx: scheme.
var uri = new System.Uri("ms-appx:///images/logo.png"); var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
Windows Runtime APIs do not support URIs of type UriKind.Relative, so you typically use the signature that infers the UriKind and make sure you've specified a valid absolute URI including the scheme and authority.
To access files stored in the app data, use the ms-appdata: scheme. App data may be stored in a local folder, a roaming folder, or a temp folder.
To access files stored in the local folder:
<Image Source="ms-appdata:///local/images/logo.png" />
To access files stored in the roaming folder:
<Image Source="ms-appdata:///roaming/images/logo.png" />
To access files stored in the temp folder:
<Image Source="ms-appdata:///temp/images/logo.png" />
The storage file APIs can access files inside the app package in the same manner:
var uri = new System.Uri("ms-appdata:///local/images/logo.png"); var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
Apps may be running when the system changes, such as when the user turns on high contrast. This results in the app using a different set of qualifiers. Various system changes will invoke events on the ResourceContext object, and the resources might be re-evaluated, depending on the implementations of the Windows Runtime classes your app uses.
XAML also has a related concept of a ThemeResource. This is a different resource metaphor, where UI-related value types such as brushes, text and measurements that can be defined as shareable in XAML will automatically switch values and reevaluate when the user switches themes. For more info, see ResourceDictionary and XAML resource references.