Preload Theme Resources
- 4 minutes to read
Controls use theme resources (default styles, templates, keys) to change their appearance when you apply a DevExpress WPF Theme to the application.
A WPF application loads a control’s theme resources only when the control is about to be displayed. As a result, windows that rely on DevExpress WPF themes can take longer to appear.
This prolonged display time can be crucial when the first window of your application is “lightweight” (for example, a login form), and the main application window is “heavy” (it contains a large control or many controls). In this case, the first window appears quickly and the second one takes longer to load theme resources.
The ApplicationThemeHelper.Preload and ApplicationThemeHelper.PreloadAsync methods allow you to speed up the display time of subsequent windows. These methods preload theme resources for the specified DevExpress controls or the entire UserControl.
Refer to the following method overloads for more information: ApplicationThemeHelper.Preload / ApplicationThemeHelper.PreloadAsync.
Synchronous Theme Resource Preload
When to Use
This technique works best when you display the Splash Screen Manager at the application startup. In this case, the method loads theme resources while a splash screen is displayed.
Performance
Theme preload slows down application startup but speeds up startup of subsequent windows that use preloaded theme resources.
Example
Call the ApplicationThemeHelper.Preload method to preload theme resources synchronously.
The following code sample preloads Data Grid and Layout Control assemblies while the Splash Screen Manager is displayed:
using DevExpress.Xpf.Core;
using System.Runtime.CompilerServices;
using System.Windows;
public partial class App : Application {
static App() {
PreloadThemes();
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void PreloadThemes() {
SplashScreenManager.CreateThemed().ShowOnStartup();
ApplicationThemeHelper.Preload(PreloadCategories.Grid, PreloadCategories.LayoutControl);
}
}
Asynchronous Theme Resource Preload
When to Use
Asynchronous theme preload works best when the application start window is “lightweight”, like a login form. In this case, you can use the ApplicationThemeHelper.PreloadAsync (for lightweight themes) or PreloadThemeResourceAsync (for regular themes) method to preload theme resources of the second window asynchronously while a user inputs login credentials.
Asynchronous theme preload may slow down the first window startup if this window is “heavy” and you do not preload its resources. Use one of the following techniques to avoid this behavior:
- Asynchronously preload this window resources during the startup.
- Start to preload subsequent window resources after the first window is shown as demonstrated in following code sample: Asynchronous Theme Preload on Dispatcher.
Example: Lightweight Themes
The following code sample preloads Data Grid and Layout Control theme resources asynchronously at the application startup:
using DevExpress.Xpf.Core;
using System.Windows;
public partial class App : Application {
static App() {
CompatibilitySettings.UseLightweightThemes = true;
}
protected override async void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
await ApplicationThemeHelper.PreloadAsync(PreloadCategories.Grid, PreloadCategories.LayoutControl);
}
}
Asynchronous Theme Preload on Dispatcher
Use this technique if the first application window is “heavy” and you do not want to slow down the first application window loading.
You can start the asynchronous theme resource preload after the first application window is displayed (on the Dispatcher). In this case, preload does not slow down resource loading from the UI thread of the first application window (as the asynchronous theme resource preload does).
The following code sample starts the theme resource preload after the first application window is displayed:
using DevExpress.Xpf.Core;
using System.Windows;
using System.Windows.Threading;
public partial class App : Application {
static App() {
CompatibilitySettings.UseLightweightThemes = true;
}
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
Dispatcher.CurrentDispatcher.BeginInvoke(
() => {
ApplicationThemeHelper.PreloadAsync(PreloadCategories.Grid, PreloadCategories.LayoutControl);
},
DispatcherPriority.Render
);
}
}
Example: Regular Themes
The following code sample preloads Office2019Colorful theme resources asynchronously at the application startup:
using System.Windows;
using System.Threading;
using DevExpress.Xpf.Core;
public partial class App : Application {
static App() {
CompatibilitySettings.AllowThemePreload = true;
}
protected override async void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
await ThemeManager.PreloadThemeResourceAsync("Office2019Colorful");
}
}
Set the CompatibilitySettings.AllowThemePreload property to true
to enable the legacy theme preload.
Refer to the following section for more information on how to asynchronously preload regular themes on Dispatcher: Use Asynchronous Theme Preload on Dispatcher.