icsharpcode/ILSpy · error · Exception

Cannot find StaticResource @{position}

Error message

Cannot find StaticResource @{position}

What it means

Thrown by PropertyWithStaticResourceIdHandler.Translate when XamlResourceKey.FindKeyInAncestors returns null. The record's StaticResourceId has no enclosing resource key in the ancestor block tree that owns any static resources, so the reference cannot be resolved.

Source

Thrown at ICSharpCode.BamlDecompiler/Handlers/Records/PropertyWithStaticResourceIdHandler.cs:54

		{
			var record = (PropertyWithStaticResourceIdRecord)((BamlRecordNode)node).Record;
			var doc = new BamlElement(node);

			var elemAttr = ctx.ResolveProperty(record.AttributeId);
			doc.Xaml = new XElement(elemAttr.ToXName(ctx, null));

			doc.Xaml.Element.AddAnnotation(elemAttr);
			parent.Xaml.Element.Add(doc.Xaml.Element);

			BamlNode found = node;
			XamlResourceKey key;
			do
			{
				key = XamlResourceKey.FindKeyInAncestors(found.Parent, out found);
			} while (key != null && record.StaticResourceId >= key.StaticResources.Count);

			if (key == null)
				throw new Exception("Cannot find StaticResource @" + node.Record.Position);

			var resNode = key.StaticResources[record.StaticResourceId];

			var handler = (IDeferHandler)HandlerMap.LookupHandler(resNode.Type);
			var resElem = handler.TranslateDefer(ctx, resNode, doc);

			doc.Children.Add(resElem);
			resElem.Parent = doc;

			elemAttr.DeclaringType.ResolveNamespace(doc.Xaml, ctx);
			doc.Xaml.Element.Name = elemAttr.ToXName(ctx, null);

			return doc;
		}
	}
}

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Inspect the record position reported in the message to locate the dangling reference.
  2. Re-extract from a clean assembly.
  3. Report as a malformed resource.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    handler.Translate(ctx, node, parent);
} catch (Exception ex) when (ex.Message.StartsWith("Cannot find StaticResource")) {
    // dangling StaticResourceId; skip this node
}

Prevention

When it happens

Trigger: A PropertyWithStaticResourceId record appears with no matching StaticResourceStart/... resource key among its ancestors (or the ancestor key has fewer StaticResources than the id).

Common situations: Malformed BAML with a dangling static-resource reference; missing StaticResourceStart block; resource table built out of order.

Related errors


AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13). Data as JSON: /api/errors/20432f6c703d6410. Report an issue: GitHub.