stride3d/stride · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values…

Error message

Specified argument was out of range of valid values. (Parameter 'measurementMethod')

What it means

MeasureText selects a text measurement strategy via a TextMeasurementMethod enum. If the supplied value is not one of the defined enum members, the switch statement falls through to default and throws ArgumentOutOfRangeException. This typically happens with an invalid cast or a value from a newer/older assembly version.

Solutions

  1. Pass a valid TextMeasurementMethod enum member; validate config-sourced values with Enum.IsDefined before casting.
  2. Replace unsafe int-to-enum casts with Enum.TryParse and a fallback default.
  3. Check that all projects reference the same Stride assembly version.

Example fix

// before
var method = (TextMeasurementMethod)settings.Method;
var size = renderer.MeasureText(text, font, size, weight, method);
// after
TextMeasurementMethod method = Enum.IsDefined(typeof(TextMeasurementMethod), settings.Method)
    ? (TextMeasurementMethod)settings.Method
    : TextMeasurementMethod.TextBlock;
var size = renderer.MeasureText(text, font, size, weight, method);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(TextMeasurementMethod), method)) method = TextMeasurementMethod.TextBlock;

Type guard

bool IsValidMethod(TextMeasurementMethod m) => Enum.IsDefined(typeof(TextMeasurementMethod), m);

Try / catch

try { size = renderer.MeasureText(text, font, fontSize, weight, method); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "measurementMethod") { size = renderer.MeasureText(text, font, fontSize, weight, TextMeasurementMethod.TextBlock); }

Prevention

When it happens

Trigger: Calling MeasureText with an out-of-range TextMeasurementMethod, e.g. an unchecked cast like (TextMeasurementMethod)99 or an enum value from a different library version.

Common situations: Storing the method in config as an int and casting without validating; assembly version mismatch where the saved enum value no longer exists; copy-paste of a similarly named enum.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Drawing/CanvasRenderer.cs:330

                    GlyphTypeface glyphTypeface;
                    if (TryGetGlyphTypeface(fontFamily, FontStyles.Normal, fontWeight, FontStretches.Normal, out glyphTypeface))
                        return MeasureTextSize(glyphTypeface, fontSize, text);
                    // Fallback to TextBlock measurement method
                    goto case TextMeasurementMethod.TextBlock;

                case TextMeasurementMethod.TextBlock:
                    var textBlock = new TextBlock
                    {
                        FontFamily = fontFamily,
                        FontSize = fontSize,
                        FontWeight = fontWeight,
                        Text = text,
                    };
                    textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    return new Size(textBlock.DesiredSize.Width, textBlock.DesiredSize.Height);

                default:
                    throw new ArgumentOutOfRangeException(nameof(measurementMethod));
            }
        }

        /// <inheritdoc/>
        public Size MeasureTexts(IList<string> texts, FontFamily fontFamily, double fontSize, FontWeight fontWeight, TextMeasurementMethod measurementMethod)
        {
            if (texts == null) throw new ArgumentNullException(nameof(texts));

            var maxWidth = 0.0;
            var maxHeight = 0.0;
            TextBlock textBlock = null;
            for (var i = 0; i < texts.Count; ++i)
            {
                var text = texts[i];
                Size size;
                switch (measurementMethod)
                {
                    case TextMeasurementMethod.GlyphTypeface:

View on GitHub (pinned to 96fad776d2)