PrismLibrary/Prism · error · InvalidOperationException

Resources.ContentViewHasContentException

Error message

Resources.ContentViewHasContentException

What it means

ContentViewRegionAdapter<TContentView>.Adapt throws when the target ContentView already has Content assigned (directly or via the BindableProperty). The adapter will set Content from region.ActiveViews, so pre-existing content conflicts with region-managed content.

Solutions

  1. Remove the XAML child content / Content assignment from the ContentView acting as a region.
  2. Or remove the region name and keep the static content if region behavior is not needed.
  3. Inject views into the region via IRegionManager instead of declaring them inline.

Example fix

<!-- before -->
<ContentView prism:RegionManager.RegionName="DetailRegion">
    <Label Text="Placeholder" />
</ContentView>

<!-- after -->
<ContentView prism:RegionManager.RegionName="DetailRegion" />
Defensive patterns

Strategy: validation

Validate before calling

bool regionOk = contentView.Content is null && !contentView.IsSet(ContentView.ContentProperty);
if (!regionOk) throw new InvalidOperationException("Remove Content before using the ContentView as a region.");

Try / catch

try { regionManager.RegisterRegions(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Content"))
{
    // clear XAML content or remove the region name
}

Prevention

When it happens

Trigger: Marking a ContentView with prism:RegionManager.RegionName while it also declares child content in XAML (<ContentView>...views...</ContentView>) or sets Content in code.

Common situations: Adding a region name to a ContentView that already hosts static XAML content; template pages where a default placeholder content was left in place.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Adapters/ContentViewRegionAdapter{TContentView}.cs:35

    public ContentViewRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
        : base(regionBehaviorFactory)
    {
    }

    /// <summary>
    /// Adapts a <see cref="ContentView"/> to an <see cref="IRegion"/>.
    /// </summary>
    /// <param name="region">The new region being used.</param>
    /// <param name="regionTarget">The object to adapt.</param>
    protected override void Adapt(IRegion region, TContentView regionTarget)
    {
        ArgumentNullException.ThrowIfNull(region);
        ArgumentNullException.ThrowIfNull(regionTarget);

        bool contentIsSet = regionTarget.Content != null || regionTarget.IsSet(ContentView.ContentProperty);

        if (contentIsSet)
            throw new InvalidOperationException(Resources.ContentViewHasContentException);

        region.ActiveViews.CollectionChanged += delegate
        {
            regionTarget.Content = region.ActiveViews.FirstOrDefault() as View;
        };

        region.Views.CollectionChanged +=
            (sender, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add && region.ActiveViews.Count() == 0)
                {
                    region.Activate(e.NewItems[0] as VisualElement);
                }
            };
    }

    /// <summary>
    /// Creates a new instance of <see cref="SingleActiveRegion"/>.

View on GitHub (pinned to 358118cd64)