dotnet/wpf · error · ArgumentException

SR.Format(SR.InputMethod_InvalidSentenceMode, value)

Error message

SR.Format(SR.InputMethod_InvalidSentenceMode, value)

What it means

InputMethod.SentenceMode setter throws ArgumentException with SR.InputMethod_InvalidSentenceMode when the value is not a valid ImeSentenceModeValues combination, as determined by IsValidSentenceMode. The sentence mode configures IME sentence-conversion behavior and is validated before being forwarded to Cicero.

Solutions

  1. Assign only values from the ImeSentenceModeValues enum (None, Automatic, Prediction, Conversation).
  2. Validate restored settings: if not ((v & ~ImeSentenceModeValues.None) == 0 || defined flags) reset to ImeSentenceModeValues.None.
  3. Use try-catch for ArgumentException when applying user-provided settings and fall back to the default mode.

Example fix

// before
ime.SentenceMode = (ImeSentenceModeValues)userSetting;
// after
var v = (ImeSentenceModeValues)userSetting;
ime.SentenceMode = Enum.IsDefined(typeof(ImeSentenceModeValues), v)
    ? v : ImeSentenceModeValues.Automatic;
Defensive patterns

Strategy: validation

Validate before calling

static readonly ImeSentenceModeValues ValidSentenceMask =
    ImeSentenceModeValues.None | ImeSentenceModeValues.Automatic |
    ImeSentenceModeValues.Prediction | ImeSentenceModeValues.Conversation;
if (((int)value & ~(int)ValidSentenceMask) != 0) value = ImeSentenceModeValues.Automatic;

Type guard

static bool IsValidSentenceModeValue(ImeSentenceModeValues v) =>
    Enum.IsDefined(typeof(ImeSentenceModeValues), v);

Try / catch

try { ime.SentenceMode = v; }
catch (ArgumentException) { ime.SentenceMode = ImeSentenceModeValues.Automatic; }

Prevention

When it happens

Trigger: Assigning an undefined or out-of-range ImeSentenceModeValues value to InputMethod.SentenceMode, typically a raw int cast or an OR of unrelated flags.

Common situations: Restoring saved IME settings from config or per-user storage with stale values; programmatic IME configuration for Japanese input where constants were hand-coded incorrectly.

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 dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/400921f8ba33c43f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputMethod.cs:1062

                            ret |= ImeSentenceModeValues.SingleConversion;
                        if ((sentence & NativeMethods.IME_SMODE_AUTOMATIC) != 0)
                            ret |= ImeSentenceModeValues.Automatic;
                        if ((sentence & NativeMethods.IME_SMODE_PHRASEPREDICT) != 0)
                            ret |= ImeSentenceModeValues.PhrasePrediction;
                        if ((sentence & NativeMethods.IME_SMODE_CONVERSATION) != 0)
                            ret |= ImeSentenceModeValues.Conversation;
    
                        return ret;
                    }
                }
                return ImeSentenceModeValues.None;
            }

            set
            {
                if (!IsValidSentenceMode(value))
                {
                    throw new ArgumentException(SR.Format(SR.InputMethod_InvalidSentenceMode, value));
                }

                Debug.Assert((value & ImeSentenceModeValues.DoNotCare) == 0);

                //
                // Update Cicero's sentence mode.
                //
                TextServicesCompartment compartment;
                compartment = TextServicesCompartmentContext.Current.GetCompartment(InputMethodStateType.ImeSentenceModeValues);

                if (compartment != null)
                {
                    UnsafeNativeMethods.SentenceModeFlags convmode = 0;

                    if ((value & ImeSentenceModeValues.PluralClause) != 0)
                        convmode |= UnsafeNativeMethods.SentenceModeFlags.TF_SENTENCEMODE_PLAURALCLAUSE;
                    if ((value & ImeSentenceModeValues.SingleConversion) != 0)
                        convmode |= UnsafeNativeMethods.SentenceModeFlags.TF_SENTENCEMODE_SINGLECONVERT;

View on GitHub (pinned to 81131a70a4)