-
-
Notifications
You must be signed in to change notification settings - Fork 171
Create UniversalGridLengthConverter.cs #940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
GridLength Converter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces a new UniversalGridLengthConverter class that provides comprehensive conversion between various types (strings, numbers, arrays) and MAUI's GridLength type, supporting multiple formats including pixel values, star notation, and auto sizing.
Changes:
- Added a new IValueConverter implementation with bidirectional conversion support for GridLength
- Supports multiple input formats: strings (with "px", "*", "Auto"), numeric types, and object arrays
- Includes culture-aware parsing for international number formats
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several unused imports should be removed: System.Collections.Generic, System.Linq, System.Text, and System.Threading.Tasks are not used anywhere in this file. Only System, System.Globalization are actually needed.
| using System.Collections.Generic; | |
| using System.Globalization; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Globalization; |
| // Строковые форматы | ||
| string str => ParseAnyGridLengthFormat(str, culture), | ||
|
|
||
| // Числовые типы | ||
| double d => new GridLength(d), | ||
| int i => new GridLength(i), | ||
| float f => new GridLength(f), | ||
| decimal dec => new GridLength((double)dec), | ||
|
|
||
| // Из массива [value, type] |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments are written in a non-English language (Russian). All code comments should be in English to maintain consistency with the rest of the codebase and ensure all contributors can understand them.
| // Возвращаем значение по умолчанию в зависимости от типа | ||
| if (targetType == typeof(string)) | ||
| return "Auto"; | ||
| if (targetType == typeof(double)) | ||
| return -1.0; | ||
| if (targetType == typeof(GridLength)) | ||
| return GridLength.Auto; | ||
|
|
||
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; | ||
| } | ||
|
|
||
| // Преобразуем GridLength в нужный тип | ||
| if (targetType == typeof(string)) | ||
| return GridLengthToString(gridLength, culture); | ||
|
|
||
| if (targetType == typeof(double) || targetType == typeof(float) || targetType == typeof(int)) | ||
| { | ||
| // Для числовых типов возвращаем значение только если это Absolute |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments are written in a non-English language (Russian). All code comments should be in English to maintain consistency with the rest of the codebase.
| /* | ||
|
|
||
|
|
||
| public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
| { | ||
| culture ??= CultureInfo.CurrentCulture; | ||
|
|
||
| if (value is not GridLength gridLength) | ||
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; | ||
|
|
||
| return targetType.Name switch | ||
| { | ||
| nameof(Double) or "Double" => gridLength.Value, | ||
| nameof(Single) or "Single" => (float)gridLength.Value, | ||
| nameof(Int32) or "Int32" => (int)gridLength.Value, | ||
| nameof(String) or "String" => GridLengthToString(gridLength, culture), | ||
| _ => gridLength.Value // По умолчанию double | ||
| }; | ||
| } */ | ||
|
|
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This large block of commented-out code should be removed. If this is an alternative implementation that might be needed in the future, it should either be removed entirely (version control preserves it) or documented with a clear explanation of why it's kept. Keeping dead code reduces maintainability.
| /* | |
| public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
| { | |
| culture ??= CultureInfo.CurrentCulture; | |
| if (value is not GridLength gridLength) | |
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; | |
| return targetType.Name switch | |
| { | |
| nameof(Double) or "Double" => gridLength.Value, | |
| nameof(Single) or "Single" => (float)gridLength.Value, | |
| nameof(Int32) or "Int32" => (int)gridLength.Value, | |
| nameof(String) or "String" => GridLengthToString(gridLength, culture), | |
| _ => gridLength.Value // По умолчанию double | |
| }; | |
| } */ |
|
|
||
| namespace UraniumUI.Material.Converters; | ||
|
|
||
|
|
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an unnecessary blank line after the namespace declaration. Looking at other converters in the codebase (e.g., StringIsNotNullOrEmptyConverter.cs:3-4), there should be no blank line between the namespace and class declaration.
|
|
||
| // Пробуем заменить запятую на точку | ||
| string normalized = value.Replace(',', '.'); | ||
| if (TryParseGridLength(normalized, culture, out result)) |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comma-to-dot replacement logic may not work correctly with cultures that use comma as a decimal separator. When you call TryParseGridLength with the normalized string, it still uses the culture parameter which may expect a comma. Consider using CultureInfo.InvariantCulture for the second parse attempt after normalizing to use a dot separator.
| if (TryParseGridLength(normalized, culture, out result)) | |
| if (TryParseGridLength(normalized, CultureInfo.InvariantCulture, out result)) |
| }; | ||
| } | ||
|
|
||
|
|
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an extra blank line between methods. Looking at other converters in the codebase, there should be only one blank line between methods.
| return gridLength.Value; // По умолчанию double | ||
| } | ||
|
|
||
|
|
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an extra blank line between methods. Looking at other converters in the codebase, there should be only one blank line between methods.
| { | ||
| return targetType == typeof(int) ? (int)gridLength.Value : gridLength.Value; | ||
| } | ||
| return targetType == typeof(int) ? -1 : -1.0; |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent return value for non-Absolute GridLength types. When converting back to numeric types, the method returns -1 for non-Absolute types, but GridLength.Star or GridLength.Auto could represent valid layouts. Consider documenting this behavior or throwing a NotSupportedException to make it clear that only Absolute values can be converted back to numeric types.
| return targetType == typeof(int) ? -1 : -1.0; | |
| throw new NotSupportedException("Only GridLength values with GridUnitType.Absolute can be converted to numeric types."); |
| if (targetType == typeof(GridLength)) | ||
| return gridLength; | ||
|
|
||
| return gridLength.Value; // По умолчанию double |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ConvertBack method returns gridLength.Value as a fallback at the end, but this may not match the expected targetType. If targetType is a specific type that wasn't handled by the previous conditions, returning a double (gridLength.Value) may cause type mismatch issues. Consider making this more explicit or handling all expected target types.
| return gridLength.Value; // По умолчанию double | |
| // Если тип не поддерживается явно, возвращаем значение по умолчанию для targetType | |
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; |
GridLength Converter