dotnet/wpf · error · ArgumentException

SR.Format(SR.NotAmbientType, xamlType.Name)

Error message

SR.Format(SR.NotAmbientType, xamlType.Name)

What it means

The FindAmbientValues(XamlType[], bool) overload validates that each XamlType in the types array has IsAmbient set. Supplying a non-ambient type throws ArgumentException with SR.NotAmbientType and paramName "types".

Solutions

  1. Only pass types where xamlType.IsAmbient is true — filter the array before the call.
  2. Mark the type's ambient members with [Ambient] so the schema context reports the type as ambient.
  3. Use the same XamlSchemaContext that produced the XamlTypes; ambient classification is context-dependent.

Example fix

// before
var types = new[] { xt1, xt2 }; // xt2.IsAmbient == false
FindAmbientValues(types, false);
// after
var types = new[] { xt1, xt2 }.Where(t => t.IsAmbient).ToArray();
FindAmbientValues(types, false);
Defensive patterns

Strategy: validation

Validate before calling

if (types.Any(t => !t.IsAmbient))
    throw new ArgumentException("All ceiling types must be ambient", nameof(types));

Type guard

static bool AllAmbientTypes(IEnumerable<XamlType> types) => types.All(t => t.IsAmbient);

Try / catch

try { FindAmbientValues(types, stopAfterFirst); }
catch (ArgumentException ex) when (ex.ParamName == "types")
{ /* identify non-ambient type from message and remove it */ }

Prevention

When it happens

Trigger: Requesting ambient values of given ceiling types (IAmbientProvider.GetAllAmbientValues or internal valueList path) while passing XamlTypes that the schema context does not classify as ambient.

Common situations: Custom markup-extension or schema-context code passing ordinary types as ceiling types; the type lacks [Ambient] on its ambient properties so IsAmbient is false; using a different schema context than the one that defines the type as ambient.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs:432

                        break;
                    }
                }

                lowerFrame = frame;
                frame = (ObjectWriterFrame)frame.Previous;
                Debug.Assert(frame is not null);
            }

            return retList;
        }

        private List<object> FindAmbientValues(XamlType[] types, bool stopAfterFirst)
        {
            foreach (XamlType xamlType in types)
            {
                if (!xamlType.IsAmbient)
                {
                    throw new ArgumentException(SR.Format(SR.NotAmbientType, xamlType.Name), nameof(types));
                }
            }

            List<object> retList = new List<object>();

            // Start the search for ambient properties with the parent frame.
            ObjectWriterFrame frame = _stack.PreviousFrame;

            while (frame.Depth >= 1)
            {
                foreach (XamlType type in types)
                {
                    object inst = frame.Instance;

                    if (frame.XamlType is not null && frame.XamlType.CanAssignTo(type))
                    {
                        if (inst is not null)
                        {

View on GitHub (pinned to 81131a70a4)