stride3d/stride · error · ArgumentException

The given id cannot be found in the root parts of this…

Error message

The given id cannot be found in the root parts of this library.

What it means

UILibraryAsset.CreateElementInstance creates a derived asset instance and remaps element ids; it then requires the requested elementId (after remapping) to be among the instance hierarchy's root parts. If it is a nested element rather than a root part, it throws ArgumentException naming elementId. Only top-level UI elements of the library can be instantiated by this API.

Solutions

  1. Pass the Guid of a root-level UIElement of the UI library asset, not a nested child.
  2. Look up the desired id in the asset's Hierarchy.RootParts to verify before calling.
  3. If you need a nested element, restructure the library so that element is a root part.

Example fix

// before
library.CreateElementInstance(childElementId, out var instanceId); // nested id
// after
var root = library.Hierarchy.RootParts.First(x => x.Name == "MyButton");
library.CreateElementInstance(root.Id, out var instanceId);
Defensive patterns

Strategy: validation

Validate before calling

bool isRoot = library.Hierarchy.RootParts.Any(x => x.Id == elementId);
if (!isRoot)
    throw new ArgumentException($"Element {elementId} is not a root part of the UI library");

Type guard

bool IsRootElement(UILibraryAsset lib, Guid id) =>
    lib.Hierarchy.RootParts.Any(x => x.Id == id);

Try / catch

try
{
    library.CreateElementInstance(elementId, out var instanceId);
}
catch (ArgumentException ex) when (ex.ParamName == "elementId")
{
    logger.Error($"Pick a root-level UIElement id: {ex.Message}");
}

Prevention

When it happens

Trigger: Calling CreateElementInstance with the Guid of a UIElement that exists in the library but is not a direct root part of the hierarchy (e.g. a child inside a grid/panel element).

Common situations: Picking a nested UIElement id from the designer and passing it to this method; element was moved under another parent after code referenced its id; refactoring removed the element from root level.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/092696224891b141. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Assets/UI/UILibraryAsset.cs:69

        /// <summary>
        /// Creates a instance of the given control that can be added to another <see cref="UIAssetBase"/>.
        /// </summary>
        /// <param name="targetContainer">The container in which the instance will be added.</param>
        /// <param name="targetLocation">The location of this asset.</param>
        /// <param name="elementId">The id of the element to instantiate.</param>
        /// <param name="instanceId">The identifier of the created instance.</param>
        /// <returns>An <see cref="AssetCompositeHierarchyData{UIElementDesign, UIElement}"/> containing the cloned elements of </returns>
        /// <remarks>This method will update the <see cref="Asset.BaseParts"/> property of the <see paramref="targetContainer"/>.</remarks>
        [NotNull]
        public AssetCompositeHierarchyData<UIElementDesign, UIElement> CreateElementInstance(UIAssetBase targetContainer, [NotNull] string targetLocation, Guid elementId, out Guid instanceId)
        {
            // TODO: make a common base method in AssetCompositeHierarchy - the beginning of the method is similar to CreatePrefabInstance
            var idRemapping = new Dictionary<Guid, Guid>();
            var instance = (UILibraryAsset)CreateDerivedAsset(targetLocation, out idRemapping);

            var rootElementId = idRemapping[elementId];
            if (instance.Hierarchy.RootParts.All(x => x.Id != rootElementId))
                throw new ArgumentException(@"The given id cannot be found in the root parts of this library.", nameof(elementId));

            instanceId = instance.Hierarchy.Parts.Values.FirstOrDefault()?.Base?.InstanceId ?? Guid.NewGuid();

            var result = new AssetCompositeHierarchyData<UIElementDesign, UIElement>();
            result.RootParts.Add(instance.Hierarchy.Parts[rootElementId].UIElement);
            result.Parts.Add(instance.Hierarchy.Parts[rootElementId]);
            foreach (var element in this.EnumerateChildPartDesigns(instance.Hierarchy.Parts[rootElementId], instance.Hierarchy, true))
            {
                result.Parts.Add(element);
            }
            return result;
        }
    }
}

View on GitHub (pinned to 96fad776d2)