unoplatform/uno · error · InvalidOperationException

Namespace must be set before calling ToString method.

Error message

Namespace must be set before calling ToString method.

What it means

Thrown by XamlTypeName.ToString(INamespacePrefixLookup) when the Namespace property is null. ToString reconstructs a qualified XAML name string (e.g. {ns}Name) and cannot produce a valid result without a namespace, so it fails fast rather than silently emitting a malformed name.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeName.cs:241

				var l = new List<XamlTypeName> ();
				l.AddRange (typeArguments);
				TypeArguments = l;
			}
		}

		public string Name { get; set; }
		public string Namespace { get; set; }
		public IList<XamlTypeName> TypeArguments { get; private set; }

		public override string ToString ()
		{
			return ToString (null);
		}

		public string ToString (INamespacePrefixLookup prefixLookup)
		{
			if (Namespace == null)
				throw new InvalidOperationException ("Namespace must be set before calling ToString method.");
			if (Name == null)
				throw new InvalidOperationException ("Name must be set before calling ToString method.");

			string ret;
			if (prefixLookup == null)
				ret = String.Concat ("{", Namespace, "}", Name);
			else {
				string p = prefixLookup.LookupPrefix (Namespace);
				if (p == null)
					throw new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, "Could not lookup prefix for namespace '{0}'", Namespace));
				ret = p.Length == 0 ? Name : p + ":" + Name;
			}
			string arr = null;
			if (ret [ret.Length - 1] == ']') {
				int idx = ret.LastIndexOf ('[');
				arr = ret.Substring (idx);
				ret = ret.Substring (0, idx);
			}

View on GitHub (pinned to 0418340488)

Solutions

  1. Always set Namespace before calling ToString: var tn = new XamlTypeName { Name = "Foo", Namespace = "http://schemas.example.com" };
  2. Use the two-or-three-argument constructor XamlTypeName(xamlNamespace, name) which sets Namespace explicitly.
  3. Guard the call site: if (tn.Namespace != null) tn.ToString();

Example fix

// before
var tn = new XamlTypeName();
tn.Name = "Button";
Console.WriteLine(tn.ToString());

// after
var tn = new XamlTypeName("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Button");
Console.WriteLine(tn.ToString());
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(tn.Namespace)) throw new InvalidOperationException("Namespace required");
var s = tn.ToString();

Type guard

static bool IsRenderable(XamlTypeName tn) => !string.IsNullOrEmpty(tn.Namespace);

Try / catch

try { return tn.ToString(prefixLookup); }
catch (InvalidOperationException) { return null; }

Prevention

When it happens

Trigger: Calling ToString() (or the parameterized overload) on an XamlTypeName created via the default constructor XamlTypeName() where Namespace was never assigned. Or constructing with new XamlTypeName(null, "Name").

Common situations: Using the default constructor then setting only Name (forgetting Namespace). Debug/logging code that calls ToString() on a partially-constructed XamlTypeName. Deserialization or reflection code that builds XamlTypeName incrementally.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/e4792238e1b8db38. Report an issue: GitHub.