{"record":{"id":"b3b59362f1a5d758","repo":"dotnet/maui","slug":"call-gtkthemes-init-before-this","errorCode":null,"errorMessage":"call GtkThemes.Init() before this","messagePattern":"call GtkThemes\\.Init\\(\\) before this","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/GTK/GtkThemes.cs","lineNumber":34,"sourceCode":"\n\t\tpublic static void Init()\n\t\t{\n\t\t\tif (IsInitialized)\n\t\t\t\treturn;\n\n\t\t\tif (PlatformHelper.GetGTKPlatform() == GTKPlatform.Windows)\n\t\t\t\tCheckWindowsGtk();\n\n\t\t\tIsInitialized = true;\n\t\t}\n\n\t\tpublic static void LoadCustomTheme(string filename)\n\t\t{\n\t\t\tif (string.IsNullOrEmpty(filename))\n\t\t\t\treturn;\n\n\t\t\tif (!IsInitialized)\n\t\t\t\tthrow new InvalidOperationException(\"call GtkThemes.Init() before this\");\n\n\t\t\t// GTK provides resource file mechanism for configuring various aspects of the operation of a GTK program at runtime. \n\t\t\t// Parses resource information from a string to allow change the App appearance.\n\t\t\tRc.Parse(filename);\n\t\t}\n\n\t\tprivate static bool CheckWindowsGtk()\n\t\t{\n\t\t\tstring location = null;\n\t\t\tVersion version = null;\n\t\t\tVersion minVersion = new Version(2, 12, 22);\n\n\t\t\tusing (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@\"SOFTWARE\\Xamarin\\GtkSharp\\InstallFolder\"))\n\t\t\t{\n\t\t\t\tif (key != null)\n\t\t\t\t\tlocation = key.GetValue(null) as string;\n\t\t\t}\n\t\t\tusing (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@\"SOFTWARE\\Xamarin\\GtkSharp\\Version\"))","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/GTK/GtkThemes.cs#L16-L52","documentation":"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.","triggerScenarios":"Calling GtkThemes.LoadCustomTheme(path) before GtkThemes.Init(). The Init call sets IsInitialized and runs platform checks (e.g. CheckWindowsGtk on Windows).","commonSituations":"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.","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."],"exampleFix":"// before\nGtkThemes.LoadCustomTheme(\"mytheme.gtkrc\"); // throws\nGtkThemes.Init();\n// after\nGtk.Application.Init();\nGtkThemes.Init();\nGtkThemes.LoadCustomTheme(\"mytheme.gtkrc\");","handlingStrategy":"validation","validationCode":"static void SafeLoadTheme(string path)\n{\n    if (!GtkThemes.IsInitialized) GtkThemes.Init();\n    GtkThemes.LoadCustomTheme(path);\n}","typeGuard":"static bool ThemesReady => GtkThemes.IsInitialized;","tryCatchPattern":null,"preventionTips":["Initialize GtkThemes before loading any theme resource.","Centralize startup ordering in one place.","Add a startup assertion on IsInitialized before theming."],"tags":["gtk","startup","init-order","theming","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}