Synchronous and asynchronous loading
Understand when localized content loads synchronously and when it must load asynchronously.
Read time 1 minuteLast updated 12 days ago
Understand when localized content loads synchronously and when it must load asynchronously.
Every accessor in Localization comes in two forms: a synchronous call that returns the value directly, and an asynchronous one that returns an and accepts a . For example, use for the synchronous form and for the asynchronous form. For more information about , refer to CancellationToken (Microsoft).
AwaitableCancellationTokenGetLocalizedStringGetLocalizedStringAsyncCancellationTokenWhen synchronous calls work
A synchronous call succeeds when everything it needs is already loaded or can load without waiting. That depends on the content source serving the value:
- Direct references are in memory after the table loads, so they resolve synchronously.
- A folder source loads both ways.
Resources - Other sources, for example a source that reads files or downloaded content, might only load asynchronously.
When a synchronous call can't produce the value, it returns nothing and you must request the content asynchronously instead.
// The synchronous call returns the value directly when the content can load synchronously. string value = scoreText.GetLocalizedString();
The loading preference
Event-driven paths, such as , components, and UI Toolkit bindings, follow the setting:
StringChangedPreferredLoadingValue | Description |
|---|---|
| Asynchronous | Values resolve in the background and arrive through events. The default. |
| Synchronous | Values resolve immediately when they can. When a value can't resolve synchronously, it falls back to the asynchronous path rather than failing. |
Synchronous loading keeps UI updates on the same frame, but blocks on any load the source performs.
Initialization
The first request triggers initialization automatically. To front-load it, for example behind a splash screen, await :
InitializeAsyncawait LocalizationSettings.InitializeAsync();
Localization raises when initialization finishes.
InitializationCompleted