dotnet/maui · error · InvalidOperationException
call GtkThemes.Init() before this
Error message
call GtkThemes.Init() before this
What it means
GtkThemes.LoadCustomTheme parses a GTK resource (.rc) file to re-skin the app. It requires GtkThemes.Init() to have run (IsInitialized flag) and throws InvalidOperationException('call GtkThemes.Init() before this') otherwise, because Rc.Parse depends on GTK's resource system being initialized.
Solutions
- Call GtkThemes.Init() at startup, before any LoadCustomTheme call.
- Keep theme loading after both Gtk.Application.Init() and GtkThemes.Init().
- If unsure of order, guard: `if (!GtkThemes.IsInitialized) GtkThemes.Init();` before loading — but prefer explicit ordering.
Example fix
// before
GtkThemes.LoadCustomTheme("mytheme.gtkrc"); // throws
GtkThemes.Init();
// after
Gtk.Application.Init();
GtkThemes.Init();
GtkThemes.LoadCustomTheme("mytheme.gtkrc"); Defensive patterns
Strategy: validation
Validate before calling
static void SafeLoadTheme(string path)
{
if (!GtkThemes.IsInitialized) GtkThemes.Init();
GtkThemes.LoadCustomTheme(path);
} Type guard
static bool ThemesReady => GtkThemes.IsInitialized;
Prevention
- Initialize GtkThemes before loading any theme resource.
- Centralize startup ordering in one place.
- Add a startup assertion on IsInitialized before theming.
When it happens
Trigger: Calling GtkThemes.LoadCustomTheme(path) before GtkThemes.Init(). The Init call sets IsInitialized and runs platform checks (e.g. CheckWindowsGtk on Windows).
Common situations: App startup ordering where a theme is loaded in a static constructor or before the Init call; refactored startup that moved LoadCustomTheme ahead of Init; copy-paste from a sample that omitted Init.
Related errors
- call Forms.Init() before this
- Glx entry point not found!
- InsertPageBefore is not supported globally on GTK, please…
- MaximumDate must be greater than MinimumDate
- MinimumDate must be lower than MaximumDate
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/b3b59362f1a5d758.
Report an issue: GitHub.
Appendix: source
Thrown at src/Compatibility/Core/src/GTK/GtkThemes.cs:34
public static void Init()
{
if (IsInitialized)
return;
if (PlatformHelper.GetGTKPlatform() == GTKPlatform.Windows)
CheckWindowsGtk();
IsInitialized = true;
}
public static void LoadCustomTheme(string filename)
{
if (string.IsNullOrEmpty(filename))
return;
if (!IsInitialized)
throw new InvalidOperationException("call GtkThemes.Init() before this");
// GTK provides resource file mechanism for configuring various aspects of the operation of a GTK program at runtime.
// Parses resource information from a string to allow change the App appearance.
Rc.Parse(filename);
}
private static bool CheckWindowsGtk()
{
string location = null;
Version version = null;
Version minVersion = new Version(2, 12, 22);
using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Xamarin\GtkSharp\InstallFolder"))
{
if (key != null)
location = key.GetValue(null) as string;
}
using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Xamarin\GtkSharp\Version"))View on GitHub (pinned to f377ff1c5e)