Udostępnij za pośrednictwem


ResourceLoader Class

Definition

Provides simplified access to app resources such as app UI strings.

Equivalent WinUI 2 API for UWP: Microsoft.Windows.ApplicationModel.Resources.ResourceLoader (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

public ref class ResourceLoader sealed
/// [Windows.Foundation.Metadata.Activatable(Windows.ApplicationModel.Resources.IResourceLoaderFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class ResourceLoader final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(Windows.ApplicationModel.Resources.IResourceLoaderFactory, 65536, "Windows.Foundation.UniversalApiContract")]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class ResourceLoader final
[Windows.Foundation.Metadata.Activatable(typeof(Windows.ApplicationModel.Resources.IResourceLoaderFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class ResourceLoader
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.ApplicationModel.Resources.IResourceLoaderFactory), 65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class ResourceLoader
function ResourceLoader(name)
Public NotInheritable Class ResourceLoader
Inheritance
Object Platform::Object IInspectable ResourceLoader
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

Use the ResourceLoader.GetString method to get the string resource identified by "Farewell".

var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
var text = resourceLoader.GetString("Farewell");
auto resourceLoader{ Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView() };
winrt::hstring text{ resourceLoader.GetString(L"Farewell") };
auto resourceLoader = Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView();
String^ text = resourceLoader->GetString("Farewell");

Remarks

A ResourceLoader object encapsulates a particular ResourceMap and a ResourceContext, combined in a simple API.

A specific resource map can be specified when the ResourceLoader is obtained; if no resource map is specified, the ResourceLoader will provide access to the "Resources" subtree of the app's main resource map.

In general, resources can be sensitive to scale or other display characteristics. For that reason, resource contexts are generally associated with a specific view. Since a ResourceLoader encapsulates a resource context, this also applies to a ResourceLoader. A ResourceLoader obtained using GetForCurrentView has a resource context associated with a view in which it was obtained. A view-independent ResourceLoader can also be obtained using GetForViewIndependentUse

  • Obtain the scale factor for the current view by using the DisplayInformation class instead of the deprecated DisplayProperties class.
  • Apps that set explicit width and height of elements should still work fine, other than possibly displaying blurry images.
  • Obtain resources that are not associated with any view by getting a ResourceLoader via GetForViewIndependentUse.

Also see Screen sizes and breakpoints.

Version history

Windows version SDK version Value added
1903 18362 GetForUIContext

Constructors

ResourceLoader()

Note

ResourceLoader() may be altered or unavailable for releases after Windows 8.1. Instead, use GetForCurrentView().

Constructs a new ResourceLoader object for the "Resources" subtree of the currently running app's main ResourceMap.

Equivalent WinUI 2 API for UWP: ResourceLoader() (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

ResourceLoader(String)

Note

ResourceLoader(String) may be altered or unavailable for releases after Windows 8.1. Instead, use GetForCurrentView(String).

Constructs a new ResourceLoader object for the specified ResourceMap.

Equivalent WinUI 2 API for UWP: ResourceLoader(String) (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

Methods

GetDefaultPriPath(String)

Provides the location of the resources.pri file for the app.

GetForCurrentView()

Gets a ResourceLoader object for the Resources subtree of the currently running app's main ResourceMap. This ResourceLoader uses a default context associated with the current view.

GetForCurrentView(String)

Gets a ResourceLoader object for the specified ResourceMap. This ResourceLoader uses a default context associated with the current view.

GetForUIContext(UIContext)

Gets a ResourceLoader object for the specified UIContext.

GetForViewIndependentUse()

Gets a ResourceLoader object for the Resources subtree of the currently running app's main ResourceMap. This ResourceLoader uses a default context that's not associated with any view.

GetForViewIndependentUse(String)

Gets a ResourceLoader object for the specified ResourceMap. This ResourceLoader uses a default context that's not associated with any view.

GetString(String)

Returns the most appropriate string value of a resource, specified by resource identifier, for the default ResourceContext of the view in which the ResourceLoader was obtained using ResourceLoader.GetForCurrentView.

Equivalent WinUI 2 API for UWP: Microsoft.Windows.ApplicationModel.Resources.ResourceLoader.GetString (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

GetStringForReference(Uri)

Note

GetStringForReference may be altered or unavailable for releases after Windows 8.1. Instead, use GetStringForUri.

Returns the most appropriate string value of a resource, specified as a Uri for a resource identifier, for the default ResourceContext of the currently running app.

GetStringForUri(Uri)

Returns the most appropriate string value of a resource, specified by a Uniform Resource Identifier (URI) resource identifier, for the default ResourceContext of the currently running app.

Equivalent WinUI 2 API for UWP: Microsoft.Windows.ApplicationModel.Resources.ResourceLoader.GetStringForUri (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

Applies to

See also