A WPF app that runs perfectly in Debug can throw IOException: Cannot locate resource 'views/mainwindow.xaml' the first time you launch its obfuscated build — before a single line of your view-model code executes. The window never opens. Nothing in your XAML bindings is wrong, and preservePublicApi is on. The break is one level below the binding layer most WPF obfuscation advice stops at: it is in how WPF stores and finds your compiled markup.

The general advice — keep bound members named so {Binding CustomerName} resolves — is necessary but not sufficient. XAML is not interpreted at runtime; it is compiled to BAML and embedded in a resource stream whose name and internal references are coupled to your assembly in ways a renamer can quietly sever. This post is the mechanism: where your XAML actually lives in the shipped DLL, which names the runtime hardcodes, and how Nebula.NET obfuscates the internals hard while leaving those links intact.

Where your XAML goes

When you build a WPF project, each .xaml file with the Page build action is compiled to BAML — a binary tokenized form of the markup — and all of those streams are packed into a single managed resource named after the assembly: MyApp.g.resources. Inside it, each window or control is keyed by its lowercased project-relative path with a .baml extension, for example views/mainwindow.baml.

The generated half of your window class reaches that stream through a pack URI:

// MainWindow.g.cs — generated, normally hidden public void InitializeComponent() { if (_contentLoaded) return; _contentLoaded = true; var uri = new Uri("/MyApp;component/views/mainwindow.xaml", UriKind.Relative); Application.LoadComponent(this, uri); } 

That one URI string carries two load-bearing names. MyApp is the assembly short name — WPF turns ;component into a lookup for MyApp.g.resources. views/mainwindow.xaml is the resource path, which WPF maps to the views/mainwindow.baml entry inside that stream. The same shape appears everywhere XAML references another resource:

<!-- App.xaml: merge a theme dictionary --> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MyApp;component/Themes/Colors.xaml"/> </ResourceDictionary.MergedDictionaries> <!-- An image shipped with Build Action = Resource --> <Image Source="pack://application:,,,/Images/logo.png"/> 

Every one of these resolves through the same two coordinates: the assembly short name and a resource path. Rename either without updating the other and the resolve fails.

The three ways obfuscation breaks it

Renaming the assembly. This is the classic trap. If your tooling renames the output assembly — or you set a different AssemblyName for the protected build — the stream becomes Protected.g.resources while the pack URI still says /MyApp;component/. WPF looks for a stream that no longer exists and throws at startup. The rule is simple: do not rename the WPF assembly's name. Obfuscate everything inside it; leave the assembly identity alone.

Renaming types without rewriting BAML. A BAML stream is not opaque bytes. It opens with an assembly table and a type table that name the x:Class type, every element type instantiated in the markup, and the properties assigned on them. Load-time, WPF reads those names and binds the stream to your MyApp.Views.MainWindow code-behind and its members. Rename that type in the IL metadata but leave the BAML record pointing at the old string, and the loader cannot connect the window to its class — you get a XamlParseException or a null code-behind.

Encrypting what WPF has to read as text. Resource paths inside pack URIs and x:Key resource keys inside BAML are consumed by the XAML/resource subsystem, not by your code. They are not ldstr instructions a string-encryption pass would see, so ordinary string encryption does not touch them — but any custom resource-name mangling that did would break resolution.

What Nebula.NET does for you

Nebula.NET recognizes a WPF assembly (it carries the .g.resources stream and a ThemeInfo/XmlnsDefinition footprint) and treats BAML as first-class metadata rather than an inert blob:

  • It preserves the assembly short name and the .g.resources stream name, so every ;component pack URI and every Application.LoadComponent call still resolves.
  • It parses and rewrites the BAML type and assembly tables in lockstep with the rename map. When a type referenced from markup is renamed in the metadata, its BAML record is rewritten to the same new name — the markup and the renamed IL stay coherent, exactly as token renumbering keeps call and ldstr operands coherent after a metadata rebuild.
  • It leaves resource paths and x:Key strings alone, because those are addresses WPF resolves, not secrets.

The practical configuration is the same minimal, safe surface you would use for any WPF app, plus the explicit intent to keep resources intact:

{ "inputs": ["bin/Release/net8.0/MyApp.dll"], "outputDirectory": "protected", "preservePublicApi": true, "preserveResources": true, "controlFlowObfuscation": true, "encryptStrings": true } 

preserveResources keeps the stream names and loose resource files addressable; preservePublicApi keeps bound view-model members named so {Binding} still resolves. Everything the markup does not name — your services, algorithms, private members — is renamed, flattened and string-encrypted at full strength. The value lives in the code behind the window, and that is exactly what gets protected.

Custom controls and themes

A reusable control library has one extra coupling: the [assembly: ThemeInfo] attribute tells WPF to look for /themes/generic.baml for default styles, and each control's DefaultStyleKey is its own Type. Because Nebula preserves the resource stream and rewrites the BAML type table coherently, Themes/Generic.xaml keeps loading and the default styles still bind — but it is the single most important screen to click through after protection, since a missing theme fails visually rather than with a hard exception.

Verify it actually loads

BAML problems surface at load, not compile, so the test is to run the protected build and open every window — especially custom-templated controls and any ResourceDictionary merged from another assembly. If a window throws, read the exception: Cannot locate resource points at a stream-name or resource-path mismatch, while XamlParseException naming a type points at a BAML type-table rewrite that did not happen.

You can also inspect the shipped artifact directly. Open the protected DLL in Glass.NET and look at the managed resources: confirm the <AssemblyName>.g.resources stream is present under the unchanged assembly name and that its BAML entries still carry the expected paths. For a deeper check, extract a BAML stream and confirm its type table names the renamed types — that is the proof the markup and the metadata agree. Automating that check in CI is the same pattern as any other obfuscation gate: protect, then assert the resource stream exists and the app's smoke test opens each window. See Verifying obfuscation in CI for the harness.

The short version

  • WPF ships your XAML as BAML inside <AssemblyName>.g.resources, and pack URIs hardcode the assembly short name and resource path.
  • Never rename the WPF assembly's name — it is baked into every ;component URI and into the resource stream name.
  • Renaming types is fine if the BAML type table is rewritten with them; Nebula.NET does this in lockstep with the rename map.
  • Keep preserveResources and preservePublicApi on, then obfuscate the internals hard — that is where the value is.
  • Test by launching and opening every window, and verify the resource stream with Glass.NET.