PrismLibrary/Prism · error · ArgumentException

The provided String argument

Error message

The provided String argument {0} must not be null or empty.

What it means

The Add(object view, string viewName) overload validates that viewName is non-null and non-empty, formatting Resources.StringCannotBeNullOrEmpty with the parameter name. A view registered by name must have a key that GetView can later look up; an empty key is meaningless. It forwards to Add(view, viewName, false) only after validation.

Solutions

  1. Pass a non-empty viewName when adding the view.
  2. If the view does not need a lookup key, use the nameless overload region.Add(view).
  3. Validate/derive the name at the call site before calling Add.

Example fix

// before
region.Add(myView, viewName); // viewName may be null/empty
// after
if (string.IsNullOrEmpty(viewName))
    viewName = nameof(MyView);
region.Add(myView, viewName);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(viewName))
    viewName = view.GetType().Name;
region.Add(view, viewName);

Type guard

bool IsValidViewName(string n) => !string.IsNullOrEmpty(n);

Try / catch

try
{
    region.Add(view, viewName);
}
catch (ArgumentException)
{
    // register without a name or with a default name
}

Prevention

When it happens

Trigger: Calling region.Add(view, "") or region.Add(view, null); passing a viewName variable built from missing metadata (e.g. control.Name or a config value that is empty).

Common situations: Dynamic view registration where the view name comes from data binding, XAML names, or settings that are not populated; refactors that dropped the hardcoded view name string.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/52286cae52c1d115. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Region.cs:243

    /// </summary>
    /// <param name="view">The view to add.</param>
    /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="VisualElement"/>. It will be the current region manager when using this overload.</returns>
    public IRegionManager Add(object view)
    {
        return Add(view, null, false);
    }

    /// <summary>
    /// Adds a new view to the region.
    /// </summary>
    /// <param name="view">The view to add.</param>
    /// <param name="viewName">The name of the view. This can be used to retrieve it later by calling <see cref="IRegion.GetView"/>.</param>
    /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="VisualElement"/>. It will be the current region manager when using this overload.</returns>
    public IRegionManager Add(object view, string viewName)
    {
        if (string.IsNullOrEmpty(viewName))
        {
            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.StringCannotBeNullOrEmpty, nameof(viewName)));
        }

        return Add(view, viewName, false);
    }

    /// <summary>
    /// Adds a new view to the region.
    /// </summary>
    /// <param name="view">The view to add.</param>
    /// <param name="viewName">The name of the view. This can be used to retrieve it later by calling <see cref="IRegion.GetView"/>.</param>
    /// <param name="createRegionManagerScope">When <see langword="true"/>, the added view will receive a new instance of <see cref="IRegionManager"/>, otherwise it will use the current region manager for this region.</param>
    /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="VisualElement"/>.</returns>
    public virtual IRegionManager Add(object view, string viewName, bool createRegionManagerScope)
    {
        IRegionManager manager = createRegionManagerScope ? RegionManager.CreateRegionManager() : RegionManager;
        InnerAdd(view, viewName, manager);
        return manager;
    }

View on GitHub (pinned to 358118cd64)