{"record":{"id":"00191e35f011be03","repo":"PrismLibrary/Prism","slug":"view-with-name-0-already-exists-in-the-region","errorCode":null,"errorMessage":"View with name '{0}' already exists in the region.","messagePattern":"View with name '(.+?)' already exists in the region\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Region.cs","lineNumber":280,"sourceCode":"\n    private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager)\n    {\n        if (view is not VisualElement visualElement)\n        {\n            throw new UpdateRegionsException(\"The view must inherit from VisualElement.\");\n        }\n\n        if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)\n        {\n            throw new InvalidOperationException(Resources.RegionViewExistsException);\n        }\n\n        var itemMetadata = new ItemMetadata(visualElement);\n        if (!string.IsNullOrEmpty(viewName))\n        {\n            if (ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null)\n            {\n                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName));\n            }\n            itemMetadata.Name = viewName;\n        }\n\n        Xaml.RegionManager.SetRegionManager(visualElement, scopedRegionManager);\n\n        ItemMetadataCollection.Add(itemMetadata);\n    }\n\n    /// <summary>\n    /// Returns the view instance that was added to the region using a specific name.\n    /// </summary>\n    /// <param name=\"viewName\">The name used when adding the view to the region.</param>\n    /// <returns>Returns the named view or <see langword=\"null\"/> if the view with <paramref name=\"viewName\"/> does not exist in the current region.</returns>\n    public virtual object GetView(string viewName)\n    {\n        if (string.IsNullOrEmpty(viewName))\n        {","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Region.cs#L262-L298","documentation":"Region.InnerAdd throws InvalidOperationException (Resources.RegionViewNameExistsException, formatted with the view name) when you add a view under a viewName that is already used by another view in the region. View names are unique per region because GetView(name) must resolve unambiguously.","triggerScenarios":"region.Add(viewA, \"details\") followed later by region.Add(viewB, \"details\") in the same region; re-running initialization code that re-registers views with the same names.","commonSituations":"Naming collisions when dynamically populating regions from a list or config; duplicate region registration in XAML + code-behind; multiple view models each registering with a shared constant name.","solutions":["Uniquify the view name before Add (e.g. append an id/index) or ensure each named view is added only once.","Remove or rename the existing view: get it via region.GetView(existingName) and region.Remove it before adding a new one under that name.","Guard with a check on region.GetView(name) == null before adding.","Use auto-generated names (leave viewName empty) if you do not need GetView-by-name."],"exampleFix":"// before\nregion.Add(new DetailsView(), \"details\"); // second call throws\n\n// after\nif (region.GetView(\"details\") == null)\n{\n    region.Add(new DetailsView(), \"details\");\n}","handlingStrategy":"validation","validationCode":"bool nameTaken = !string.IsNullOrEmpty(viewName) && region.GetView(viewName) != null;","typeGuard":null,"tryCatchPattern":"try { region.Add(view, viewName); } catch (InvalidOperationException ex) { logger.LogWarning(ex, \"View name '{Name}' already in region\", viewName); }","preventionTips":["Check region.GetView(name) == null before adding a named view","Generate unique names (index/guid suffix) when populating regions dynamically","Centralize region population in one code path to avoid duplicate registrations"],"tags":["prism","maui","regions","name-conflict"],"backgroundTag":"file-already-exists","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}