{"record":{"id":"97d94d1bae586c98","repo":"dotnet/maui","slug":"xc0004-97d94d","errorCode":"XC0004","errorMessage":"Missing default constructor for \"{0}\".","messagePattern":"Missing default constructor for \"(.+?)\"\\.","errorType":"exception","errorClass":"BuildException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/CreateObjectVisitor.cs","lineNumber":184,"sourceCode":"\t\t\tif (parameterizedCtorInfo != null && ctorInfo == null)\n\t\t\t\t//there was a parameterized ctor, we didn't use it\n\t\t\t\tthrow new BuildException(BuildExceptionCode.PropertyMissing, node, null, missingCtorParameter, typedef.FullName);\n\t\t\tvar ctorinforef = ctorInfo?.ResolveGenericParameters(typeref, Module);\n\t\t\tvar factorymethodinforef = factoryMethodInfo?.ResolveGenericParameters(typeref, Module);\n\t\t\tvar implicitOperatorref = typedef.Methods.FirstOrDefault(md =>\n\t\t\t\tmd.IsPublic &&\n\t\t\t\tmd.IsStatic &&\n\t\t\t\tmd.IsSpecialName &&\n\t\t\t\tmd.Name == \"op_Implicit\" && md.Parameters[0].ParameterType.FullName == \"System.String\");\n\n\t\t\t// The old Forms.Color was a struct, so the following BuildException wasn't thrown because IsValueType returned true.\n\t\t\t// MGColor is a class, so IsValueType is false, but there's no parameterless constructor or factory; there's a \n\t\t\t// TypeConverter. So adding this bool as a check for this condition temporarily.\n\t\t\tbool isColor = typedef.FullName == \"Microsoft.Maui.Graphics.Color\";\n\n\t\t\tif (!isColor && !typedef.IsValueType && ctorInfo == null && factoryMethodInfo == null)\n\t\t\t{\n\t\t\t\tthrow new BuildException(BuildExceptionCode.ConstructorDefaultMissing, node, null, typedef.FullName);\n\t\t\t}\n\n\t\t\tif (isColor || ctorinforef != null || factorymethodinforef != null || typedef.IsValueType)\n\t\t\t{\n\t\t\t\tVariableDefinition vardef = new VariableDefinition(typeref);\n\t\t\t\tContext.Variables[node] = vardef;\n\t\t\t\tContext.Body.Variables.Add(vardef);\n\n\t\t\t\tValueNode vnode = null;\n\t\t\t\tif (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&\n\t\t\t\t\t(vardef.VariableType.IsValueType || isColor))\n\t\t\t\t{\n\t\t\t\t\t//<Color>Purple</Color>\n\t\t\t\t\tContext.IL.Append(vnode.PushConvertedValue(Context, typeref, [typedef],\n\t\t\t\t\t\t(requiredServices) => node.PushServiceProvider(Context, requiredServices),\n\t\t\t\t\t\tfalse, true));\n\t\t\t\t\tContext.IL.Emit(Stloc, vardef);\n\t\t\t\t}","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic class MyService { public MyService(IConfiguration cfg) {...} }  <!-- no default ctor -->\n<MyService />\n// after\npublic class MyService {\n  public MyService() {}            // added default ctor\n  public MyService(IConfiguration cfg) {...}\n}","handlingStrategy":"validation","validationCode":"// A type usable in XAML should have a public parameterless ctor\nstatic bool IsXamlInstantiable(Type t) =>\n    t.IsValueType || t == typeof(Microsoft.Maui.Graphics.Color)\n    || t.GetConstructor(Type.EmptyTypes) != null;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["xaml","xamlc","constructor","default-constructor","object-construction","typeconverter","build-time"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}