PrismLibrary/Prism · error · ArgumentException

Resources.StringCannotBeNullOrEmpty

Error message

Resources.StringCannotBeNullOrEmpty

What it means

The Region.Add(object view, string viewName) overload requires a non-empty viewName; passing null or empty throws ArgumentException formatted with StringCannotBeNullOrEmpty and parameter name "viewName". The name is stored in ItemMetadata as the key for later GetView calls.

Solutions

  1. Provide a valid non-empty viewName when calling Add.
  2. If the view needs no name, use the nameless overload region.Add(view) instead.
  3. Validate the name source (config/binding) before calling Add.

Example fix

// before
region.Add(editView, viewName); // viewName may be null
// after
if (!string.IsNullOrEmpty(viewName))
    region.Add(editView, viewName);
else
    region.Add(editView);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(viewName))
    region.Add(view);
else
    region.Add(view, viewName);

Try / catch

try
{
    region.Add(view, viewName);
}
catch (ArgumentException ex)
{
    logger.LogError(ex, "viewName was null or empty.");
}

Prevention

When it happens

Trigger: Calling region.Add(myView, null) or region.Add(myView, "") — e.g. a variable viewName that was never assigned or came from an empty property.

Common situations: Programmatic view registration where the view name comes from user input, a config value, or a dictionary lookup that returned null; refactoring that dropped a literal name argument.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Wpf/Prism.Wpf/Navigation/Regions/Region.cs:262

            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>

#if !AVALONIA
        /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="DependencyObject"/>. It will be the current region manager when using this overload.</returns>
#else
        /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="AvaloniaObject"/>. It will be the current region manager when using this overload.</returns>
#endif
        public IRegionManager Add(object view, string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.StringCannotBeNullOrEmpty, "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>

#if !AVALONIA
        /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="DependencyObject"/>.</returns>
#else
        /// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="AvaloniaObject"/>.</returns>
#endif
        public virtual IRegionManager Add(object view, string viewName, bool createRegionManagerScope)

View on GitHub (pinned to 358118cd64)