Packed MainWindow.xml into assembly to prevent runtime modification

This commit is contained in:
2025-09-17 09:02:22 +02:00
parent f659ea672c
commit e329430d28
2 changed files with 8 additions and 4 deletions

View File

@@ -12,9 +12,7 @@
</ItemGroup>
<ItemGroup>
<None Update="UI/MainWindow.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<EmbeddedResource Include="UI/MainWindow.xml" />
</ItemGroup>
</Project>

View File

@@ -6,7 +6,13 @@ public class MainWindow
public MainWindow()
{
var builder = Gtk.Builder.NewFromFile("UI/MainWindow.xml");
var assembly = typeof(MainWindow).Assembly;
using var stream = assembly.GetManifestResourceStream("App.UI.MainWindow.xml");
if (stream == null)
throw new Exception("Failed to load embedded resource MainWindow.xml");
using var reader = new System.IO.StreamReader(stream);
var xml = reader.ReadToEnd();
var builder = Gtk.Builder.NewFromString(xml, -1);
var window = builder.GetObject("main_window") as Adw.Window;
if (window == null)
throw new Exception("Failed to load main_window from MainWindow.ui");