Can't get custom gresource working (original) (raw)
I’m trying to get some custom symbolic icons to work, but no matter how much I try to get them to work, they don’t work for me. It shows as a broken image.
I’ve mentioned about this on the Denaro discussion and Gir.Core discussion, but it seemed like no one really had a clue, so I decided to ask here instead
Original post:
I’m currently experimenting around with custom icons. And to achieve this, I’ve been looking into Denaro’s code and tried implementing the same result.
Unfortunately though, the custom icons I made doesn’t work in the test application I’ve developed on Ubuntu that I’ve been experimenting around with. As seen here:
So I’m really curious as to what I’m missing? I really thought I implemented everything that I could see from Denaro’s code.
Test Project.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <AllowUnsafeBlocks>true</AllowUnsafeBlocks> </PropertyGroup> <ItemGroup> <PackageReference Include="GirCore.Adw-1" Version="0.5.0" /> </ItemGroup> <Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Exec Command="glib-compile-resources --sourcedir ./Properties ./Properties/org.resource.test.gresource.xml --target=$(OutDir)/org.resource.test.gresource" /> </Target> </Project>
Program.cs
using Adw; using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Reflection; namespace Kermalis.VGMusicStudio.GTK4 { class Program { public Program() { } static void OnActivate(object sender, EventArgs e) { var win = new TestWindow(); win.SetApplication((Gtk.Application)sender); win.Present(); win.Unref(); } static void OnOpen(Gio.Application sender, Gio.Application.OpenSignalArgs args) { OnActivate(sender, args); } static int Main(string[] args) { var app = Adw.Application.New("org.resource.test", Gio.ApplicationFlags.FlagsNone); app.Register(Gio.Cancellable.GetCurrent()); GLib.Functions.Setenv("GSK_RENDERER", "cairo", false); app.OnActivate += OnActivate; if (File.Exists(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!) + "/org.resource.test.gresource")) { //Load file from program directory, required for `dotnet run` Gio.Functions.ResourcesRegister(Gio.Functions.ResourceLoad(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!) + "/org.resource.test.gresource")); } else { var prefixes = new List<string> { Directory.GetParent(Directory.GetParent(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!))!.FullName)!.FullName, Directory.GetParent(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!))!.FullName, "/usr" }; foreach (var prefix in prefixes) { if (File.Exists(prefix + "/share/org.resource.test/org.resource.test.gresource")) { Gio.Functions.ResourcesRegister(Gio.Functions.ResourceLoad(Path.GetFullPath(prefix + "/share/org.resource.test/org.resource.test.gresource"))); break; } } } app.OnOpen += OnOpen; var stat = app.Run(args.Length, args); app.Unref(); return stat; } } }
TestWindow.cs
public class TestWindow : Gtk.Window { public TestWindow() { Title = "Test Window"; DefaultWidth = 200; DefaultHeight = 200; Child = Gtk.Image.NewFromIconName("vgms-playlist-symbolic"); } }
org.resource.test/gresource.xml
<?xml version="1.0" encoding="UTF-8"?> <gresources> <gresource prefix="/org/resource/test/icons/scalable/playlist"> <file preprocess="xml-stripblanks">vgms-song-symbolic.svg</file> <file preprocess="xml-stripblanks">vgms-playlist-symbolic.svg</file> </gresource> </gresources>
Now surely I’ve got the GResource XML in the correct format, right? Because I attempted it in the same format as Denaro. But why isn’t the icons working?
If anyone would like to look into the full repo, here it is:
Custom Resource Test.zipThank you so much!
And I’ve tried using Gtk.Image.NewFromResource("vgms-playlist-symbolic")
, but it still doesn’t work.
How do I get it to work? Is there anything I need to fix up to make it work?
Okay, just after a few minutes of experimenting around, turns out using this worked:Gtk.Image.NewFromResource("/org/resource/test/icons/scalable/playlist/vgms-playlist-symbolic.svg")
I didn’t know until now that I have to apply “.svg” to the end of the filename, and place the prefix defined in the xml in there as well.
Well, I’ve learnt something now.
But there’s one problem: It’s not theme-aware, despite reading this guide.
system (system) Closed June 11, 2025, 5:57pm 4
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.