dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(prefix))

Error message

ArgumentNullException(nameof(prefix))

What it means

XmlnsPrefixAttribute's constructor throws ArgumentNullException when the prefix parameter is null. The prefix is the designer/tooltip recommendation for how to refer to the namespace in XAML; a null prefix is rejected at construction.

Solutions

  1. Pass a non-null short identifier string (e.g. "controls") as the prefix argument.
  2. If the prefix comes from a variable or config, guard against null before constructing the attribute.
  3. Keep prefix and XmlnsDefinition namespace strings in shared constants to avoid mismatches.

Example fix

// before
[assembly: XmlnsPrefix("http://schemas.myapp.com/controls", null)]
// after
[assembly: XmlnsPrefix("http://schemas.myapp.com/controls", "controls")]
Defensive patterns

Strategy: validation

Validate before calling

if (prefix is null) throw new ArgumentException("prefix must be non-null", nameof(prefix));
var attr = new XmlnsPrefixAttribute(xmlNamespace, prefix);

Type guard

bool HasPrefix(string? prefix) => !string.IsNullOrEmpty(prefix);

Try / catch

try { var attr = new XmlnsPrefixAttribute(ns, prefix); }
catch (ArgumentNullException ex) { /* ex.ParamName == "prefix": use a default prefix */ }

Prevention

When it happens

Trigger: Calling new XmlnsPrefixAttribute(someXmlNamespace, null) — an [assembly: XmlnsPrefix] declaration whose second argument is null.

Common situations: Generated or hand-written assembly attributes where the prefix string variable was never assigned; copy-paste mistakes in AssemblyInfo.cs.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8cf4dc90f9edd5dc. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/XmlnsPrefixAttribute.cs:40

    ///   <code>
    ///   &lt;Page xmlns:myns="http://schemas.fabrikam.com/mynamespace" .... &gt;
    ///      &lt;myns:MyButton&gt; ..... &lt;/myns:MyButton&gt;
    ///   &lt;/Page&gt;
    ///   </code>
    /// </summary>
    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
    [TypeForwardedFrom("WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
    public sealed class XmlnsPrefixAttribute : Attribute
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="xmlNamespace">XML namespce</param>
        /// <param name="prefix">recommended prefix</param>
        public XmlnsPrefixAttribute(string xmlNamespace, string prefix)
        {
            XmlNamespace = xmlNamespace ?? throw new ArgumentNullException(nameof(xmlNamespace));
            Prefix= prefix ?? throw new ArgumentNullException(nameof(prefix));
        }

        /// <summary>
        /// XML Namespace
        /// </summary>
        public string XmlNamespace { get; }

        /// <summary>
        /// New Xml Namespace
        /// </summary>
        public string Prefix { get; }
   }
}

View on GitHub (pinned to 81131a70a4)