.NET MAUI自定义ContentView报输入字符串格式不正确错误
问题根因
抛出"Input string was not in a correct format"错误是三处写法不符合MAUI XAML运行规则导致的:
- 放在
ContentView.Resources下的ValueConverter不属于可视化树元素,没有继承的BindingContext,直接给Converter的IsTrue/IsFalse属性写{Binding XXX}绑定,XAML编译器在资源实例化阶段找不到绑定源,解析值时触发格式转换失败。 - 自定义控件的
TrueColor/FalseColor属性定义为string类型,但BoxView.Color要求接收Microsoft.Maui.Graphics.Color类型值,XAML编译阶段做强类型校验时,无法直接将十六进制字符串解析为Color类型触发错误。 - 未给自定义控件设置自身为BindingContext,所有指向控件自身依赖属性的绑定会默认沿父级可视化树查找BindingContext,既找不到对应属性,也会触发XAML编译校验错误。
*额外注意:原代码中BindableProperty的命名不符合MAUI约定,标准命名应为「属性名+Property」后缀,不规范命名可能触发隐式的绑定解析问题。
修复方案
推荐两种可直接运行的修复方式,第二种更简洁不需要额外写Converter:
方式1:修正原有Converter逻辑
- 后台代码调整:将颜色属性类型改为Color,使用标准BindableProperty命名,构造函数中设置BindingContext为控件自身
using Microsoft.Maui.Graphics; public partial class IsPublished : ContentView { public static readonly BindableProperty TrueColorProperty = BindableProperty.Create( nameof(TrueColor), typeof(Color), typeof(IsPublished), Color.FromArgb("#08AA04")); public static readonly BindableProperty FalseColorProperty = BindableProperty.Create( nameof(FalseColor), typeof(Color), typeof(IsPublished), Color.FromArgb("#FF3D00")); public static readonly BindableProperty PublishedProperty = BindableProperty.Create( nameof(Published), typeof(bool), typeof(IsPublished), false); public Color TrueColor { get => (Color)GetValue(TrueColorProperty); set => SetValue(TrueColorProperty, value); } public Color FalseColor { get => (Color)GetValue(FalseColorProperty); set => SetValue(FalseColorProperty, value); } public bool Published { get => (bool)GetValue(PublishedProperty); set => SetValue(PublishedProperty, value); } public IsPublished() { InitializeComponent(); BindingContext = this; } }
- Converter调整:去掉无法生效的属性预传值逻辑,通过ConverterParameter传入控件实例获取颜色值
public class IsPublishedToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is bool isPublished && parameter is IsPublished control) { return isPublished ? control.TrueColor : control.FalseColor; } return Colors.Gray; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => null; }
- XAML调整:去掉资源中Converter的无效绑定,传入控件实例作为ConverterParameter
<?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Name="this" x:Class="App.Controls.IsPublished" xmlns:converter="clr-namespace:App.Converters" xmlns:local="clr-namespace:App.Controls" x:DataType="local:IsPublished"> <ContentView.Resources> <converter:IsPublishedToColorConverter x:Key="IsPublishedToColor" /> </ContentView.Resources> <BoxView CornerRadius="10" WidthRequest="20" HeightRequest="20" Color="{Binding Published, Converter={StaticResource IsPublishedToColor}, ConverterParameter={x:Reference this}}" /> </ContentView>
方式2:用DataTrigger替代Converter(推荐)
这种写法完全不需要单独写Converter,也不存在资源绑定失效问题,代码更易维护:
- 后台代码和方式1一致,保留Color类型的属性和BindingContext设置
- XAML直接用触发器实现颜色切换,删掉Converter相关代码
<?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Name="this" x:Class="App.Controls.IsPublished" xmlns:local="clr-namespace:App.Controls" x:DataType="local:IsPublished"> <BoxView CornerRadius="10" WidthRequest="20" HeightRequest="20" Color="{Binding FalseColor}"> <BoxView.Triggers> <DataTrigger TargetType="BoxView" Binding="{Binding Published}" Value="True"> <Setter Property="Color" Value="{Binding TrueColor}" /> </DataTrigger> </BoxView.Triggers> </BoxView> </ContentView>
内容的提问来源于stack exchange,提问作者Joshlo
相关产品推荐
相关产品推荐

