dotnet/maui · error · ArgumentNullException

key

Error message

key

What it means

Thrown by SetDynamicResource(BindableProperty, string key, SetterSpecificity) when key is null or empty, via 'throw new ArgumentNullException(nameof(key))'. MAUI uses ArgumentNullException (rather than ArgumentException) for a missing resource key because a resource link is meaningless without it. The guard fires before OnSetDynamicResource, which would otherwise try to look up an empty key.

Source

Thrown at src/Controls/src/Core/BindableObject.cs:493

			OnRemoveDynamicResource(property);
			BindablePropertyContext context = GetOrCreateContext(property);
			context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
		}

		void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
			=> SetDynamicResource(property, key, SetterSpecificity.DynamicResourceSetter);

		internal void SetDynamicResource(BindableProperty property, string key)
			=> SetDynamicResource(property, key, SetterSpecificity.DynamicResourceSetter);

		//FIXME, use specificity
		internal void SetDynamicResource(BindableProperty property, string key, SetterSpecificity specificity)
		{
			if (property == null)
				throw new ArgumentNullException(nameof(property));
			if (string.IsNullOrEmpty(key))
				throw new ArgumentNullException(nameof(key));

			OnSetDynamicResource(property, key, specificity);
		}

		/// <summary>
		/// Sets the value of the specified bindable property.
		/// </summary>
		/// <param name="property">The bindable property on which to assign a value.</param>
		/// <param name="value">The value to set.</param>
		/// <exception cref="ArgumentNullException">Thrown when <paramref name="property"/> is <see langword="null"/>.</exception>
		/// <remarks>If <paramref name="property"/> is read-only, nothing will happen.</remarks>
		public void SetValue(BindableProperty property, object value)
		{
			if (property == null)
				throw new ArgumentNullException(nameof(property));

			if (value is BindingBase binding && !property.ReturnType.IsAssignableFrom(typeof(BindingBase)))
			{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a non-empty resource key that exists in a ResourceDictionary.
  2. Guard before calling: 'if (!string.IsNullOrEmpty(key)) obj.SetDynamicResource(prop, key);'.
  3. Fix the source of the key so it is always a real resource name.

Example fix

// before
obj.SetDynamicResource(prop, key);
// after
if (!string.IsNullOrEmpty(key))
    obj.SetDynamicResource(prop, key);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(key)) throw new ArgumentException("resource key required", nameof(key));
obj.SetDynamicResource(property, key);

Type guard

static bool IsValidResourceKey(string key) => !string.IsNullOrWhiteSpace(key);

Prevention

When it happens

Trigger: Calling SetDynamicResource with a null or empty key; a resource key read from configuration/binding that was unset; building keys from null interpolation.

Common situations: Theme keys coming from a settings object whose property was not populated; conditional resource binding where the key defaulted to empty string.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/53be024952207c3d. Report an issue: GitHub.