Dela via


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.

Web

To access files from the web in XAML, you can use a standard absolute HTTP URIs.

<Image Source="https://www.contoso.com/images/logo.png" alt="Logo" />

App package

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

Images/en-US/homeregion-USA/logo.scale-100_contrast-white.png

by referring to

Images/logo.png

To access files relative to the current XAML page, you can use a relative URIs.

<Image Source="images/logo.png" />

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.

App data

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);

Events and context changes

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.

How to load string resources

Defining app resources