Skip to main content
You are viewing help content for a version that is no longer maintained/updated.
All docs
V20.2
  • ThemeManager.PreloadThemeResourceAsync(String, CancellationToken, Boolean) Method

    Asynchronously preloads the specified theme and creates theme resources for all the loaded assemblies.

    Namespace: DevExpress.Xpf.Core

    Assembly: DevExpress.Xpf.Core.v20.2.dll

    NuGet Packages: DevExpress.WindowsDesktop.Wpf.Core, DevExpress.Wpf.Core

    Declaration

    public static Task PreloadThemeResourceAsync(
        string themeName,
        CancellationToken token = default(CancellationToken),
        bool runTypeInitializers = false
    )

    Parameters

    Name Type Description
    themeName String

    A theme you want to preload.

    Optional Parameters

    Name Type Default Description
    token CancellationToken *null*

    An object that indicates whether to cancel the action.

    runTypeInitializers Boolean False

    true to invoke static constructors; otherwise, false.

    Returns

    Type Description
    Task

    A task that allows you to asynchronously return the preloaded theme.

    Remarks

    Controls load theme resources when you run the application. An application loads theme resources for the controls displayed on the current application window. When a user opens a subsequent application window, the application loads theme resources for controls used in the window that were not loaded. This loading process may lead to UI freezes.

    You can use the PreloadThemeResourceAsync method to load and cache all theme resources for all loaded assemblies asynchronously at the application startup. The PreloadThemeResourceAsync method loads theme resources of assemblies that are loaded when the method is called.

    Tip

    You can use the Debug Modules view to get the loaded assemblies list.

    Set the runTypeInitializers method parameter to true to invoke static constructors and speed up control initialization.

    The following table demonstrates display times for the main and subsequent applications with the enabled and disabled PreloadThemeResourceAsync method:

    Mode Startup time (synchronous), ms Application load time (asynchronous), ms Subsequent window display time, ms
    PreloadThemeResourceAsync is disabled 0 0 3100
    PreloadThemeResourceAsync is enabled, runTypeInitializers is false 0 3800 1300
    PreloadThemeResourceAsync is enabled, runTypeInitializers is true 0 6800 1100
    Main Application Window 2100 0 -

    Tip

    The PreloadThemeResourceAsync method is also effective if you do not use the NGen.

    The following code sample creates static constructors and preloads the Office2019Colorful theme resources at the application startup.

    using System.Windows;
    using System.Threading;
    using DevExpress.Xpf.Core;
    
    public partial class App : Application {
        protected async override void OnStartup(StartupEventArgs e) {
            base.OnStartup(e);
            await ThemeManager.PreloadThemeResourceAsync("Office2019Colorful");
        }
    }
    

    Cancellation Token

    You can use the CancellationToken struct to cancel the PreloadThemeResourceAsync method. Create a cancellation token, pass it to the PreloadThemeResourceAsync method, and use the CancellationTokenSource.Cancel method to cancel the preload operation:

    using System.Windows;
    using System.Threading;
    using DevExpress.Xpf.Core;
    
    namespace Application {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window {
            public MainWindow() {
                InitializeComponent();
            }
    
            static CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
            CancellationToken token = cancelTokenSource.Token;
    
            private async void Button_Click(object sender, RoutedEventArgs e) {
    
                await ThemeManager.PreloadThemeResourceAsync("Office2019Colorful", token);
                var window = new Window1();
                window.ShowDialog();
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                cancelTokenSource.Cancel();
            }
        }
    }
    

    Load an Additional Assembly

    The PreloadThemeResourceAsync method loads only theme resources of assemblies that are loaded when the method is called. You can also load additional assemblies. When assemblies are added, the method preloads these assemblies and their dependencies.

    The following code sample preloads the additionally loaded DevExpress.Xpf.Grid.v20.2.dll and DevExpress.Xpf.PropertyGrid.v20.2.dll assemblies and their dependences:

    using System.Windows;
    using System.Threading;
    using DevExpress.Xpf.Core;
    
    public partial class App : Application {
        protected async override void OnStartup(StartupEventArgs e) {
            base.OnStartup(e);
    
            Assembly gridassembly = Assembly.LoadFrom("DevExpress.Xpf.Grid.v20.2.dll");
            Assembly propertygridassembly = Assembly.LoadFrom("DevExpress.Xpf.PropertyGrid.v20.2.dll");
            await ThemeManager.PreloadThemeResourceAsync("Office2019Colorful");
        }
    }
    
    See Also