Skip to content

Commit c3f2df1

Browse files
committed
Handle command names that can't be lowered
1 parent e2140f2 commit c3f2df1

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

CommunityToolkit.Mvvm.SourceGenerators/Input/RelayCommandGenerator.Execute.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,17 @@ public static (string FieldName, string PropertyName) GetGeneratedFieldAndProper
439439

440440
propertyName += "Command";
441441

442-
string fieldName = $"{char.ToLower(propertyName[0], CultureInfo.InvariantCulture)}{propertyName.Substring(1)}";
442+
char firstCharacter = propertyName[0];
443+
char loweredFirstCharacter = char.ToLower(firstCharacter, CultureInfo.InvariantCulture);
444+
445+
// The field name is generated depending on whether the first character can be lowered:
446+
// - If it can, then the field name is just the property name starting in lowercase.
447+
// - If it can't (eg. starts with '中'), then the '_' prefix is added to the property name.
448+
string fieldName = (firstCharacter == loweredFirstCharacter) switch
449+
{
450+
true => $"_{propertyName}",
451+
false => $"{loweredFirstCharacter}{propertyName.Substring(1)}"
452+
};
443453

444454
return (fieldName, propertyName);
445455
}

0 commit comments

Comments
 (0)