dotnet/maui · error · BuildException
XC0004
XC0004
Error message
Missing default constructor for "{0}". What it means
Thrown when the type is a non-value-type (and not the special-cased Microsoft.Maui.Graphics.Color), no parameterless public constructor exists, no factory method applies, and no parameterized [Parameter] constructor was satisfied. The XAML compiler cannot instantiate the object without one of these.
Source
Thrown at src/Controls/src/Build.Tasks/CreateObjectVisitor.cs:184
if (parameterizedCtorInfo != null && ctorInfo == null)
//there was a parameterized ctor, we didn't use it
throw new BuildException(BuildExceptionCode.PropertyMissing, node, null, missingCtorParameter, typedef.FullName);
var ctorinforef = ctorInfo?.ResolveGenericParameters(typeref, Module);
var factorymethodinforef = factoryMethodInfo?.ResolveGenericParameters(typeref, Module);
var implicitOperatorref = typedef.Methods.FirstOrDefault(md =>
md.IsPublic &&
md.IsStatic &&
md.IsSpecialName &&
md.Name == "op_Implicit" && md.Parameters[0].ParameterType.FullName == "System.String");
// The old Forms.Color was a struct, so the following BuildException wasn't thrown because IsValueType returned true.
// MGColor is a class, so IsValueType is false, but there's no parameterless constructor or factory; there's a
// TypeConverter. So adding this bool as a check for this condition temporarily.
bool isColor = typedef.FullName == "Microsoft.Maui.Graphics.Color";
if (!isColor && !typedef.IsValueType && ctorInfo == null && factoryMethodInfo == null)
{
throw new BuildException(BuildExceptionCode.ConstructorDefaultMissing, node, null, typedef.FullName);
}
if (isColor || ctorinforef != null || factorymethodinforef != null || typedef.IsValueType)
{
VariableDefinition vardef = new VariableDefinition(typeref);
Context.Variables[node] = vardef;
Context.Body.Variables.Add(vardef);
ValueNode vnode = null;
if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&
(vardef.VariableType.IsValueType || isColor))
{
//<Color>Purple</Color>
Context.IL.Append(vnode.PushConvertedValue(Context, typeref, [typedef],
(requiredServices) => node.PushServiceProvider(Context, requiredServices),
false, true));
Context.IL.Emit(Stloc, vardef);
}View on GitHub (pinned to f377ff1c5e)
Solutions
- Add a public parameterless constructor to the type.
- If you cannot change the type, instantiate it in code-behind or via a static x:FactoryMethod.
- Decorate a parameterized ctor's params with [Parameter] and supply them in XAML.
- If a TypeConverter should apply, ensure it has ProvideCompiledAttribute so XAML compilation uses it.
- For value types the error is suppressed; consider whether the type can be a struct, though that is a design change.
Example fix
// before
public class MyService { public MyService(IConfiguration cfg) {...} } <!-- no default ctor -->
<MyService />
// after
public class MyService {
public MyService() {} // added default ctor
public MyService(IConfiguration cfg) {...}
} Defensive patterns
Strategy: validation
Validate before calling
// A type usable in XAML should have a public parameterless ctor
static bool IsXamlInstantiable(Type t) =>
t.IsValueType || t == typeof(Microsoft.Maui.Graphics.Color)
|| t.GetConstructor(Type.EmptyTypes) != null; Prevention
- Provide a public parameterless constructor for XAML-instantiated types.
- Use x:FactoryMethod or code-behind for types lacking one.
- Add a TypeConverter with ProvideCompiledAttribute for convertible types.
When it happens
Trigger: Instantiating a class in XAML that only has non-public or parameterized constructors without [Parameter]; a third-party type not designed for XAML instantiation; a type that expects a TypeConverter but is not the Color special case.
Common situations: Trying to declare <ThirdPartyType /> in XAML where the type has no default ctor; using a model class meant for code-behind; missing the TypeConverter attribute the compiler would otherwise use.
Related errors
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/97d94d1bae586c98.
Report an issue: GitHub.