Xamarin Forms: Trying to build conditional formatting in for Android, Error: System.InvalidCastException Message=Object must implement IConvertible

Xamarin Forms: Trying to build conditional formatting in for Android, Error: System.InvalidCastException Message=Object must implement IConvertible

我有一个设置了网格的页面:

<Grid Padding="10,0,10,0"  RowSpacing="20" ColumnSpacing="10" VerticalOptions="CenterAndExpand" >
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="1" Grid.Column="0" Text="Last Updated:" Style="{DynamicResource ListItemTextStyle}"  />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding LastUpdated}" Style="{DynamicResource ListItemDetailTextStyle}" HorizontalTextAlignment="End" />
<Label Grid.Row="2" Grid.Column="0" Text="Next Payday:" Style="{DynamicResource ListItemTextStyle}"  />
<Label Grid.Row="2" Grid.Column="1" Text="{Binding NextPayday}" Style="{DynamicResource ListItemDetailTextStyle}"  HorizontalTextAlignment="End"/>
<Label Grid.Row="3" Grid.Column="0" Text="Available in account:" Style="{DynamicResource ListItemTextStyle}"  />
<Label Grid.Row="3" Grid.Column="1" Text="{Binding AvailableInAccount, StringFormat='({0:+0.0;-0.0})'}" TextColor="{Binding AvailableInAccount, Converter={StaticResource SignToBrushConverter}}" HorizontalTextAlignment="End"/>
<Label Grid.Row="4" Grid.Column="0" Text="Overdrawn/Excess:" Style="{DynamicResource ListItemTextStyle}" />
<Label Grid.Row="4" Grid.Column="1" Text="{Binding OverdrawnExcess, StringFormat='({0:+0.0;-0.0})'}" TextColor="{Binding OverdrawnExcess, Converter={StaticResource SignToBrushConverter}}" HorizontalTextAlignment="End"/>
<Label Grid.Row="5" Grid.Column="0" Text="Budgeted vs Actual:" Style="{DynamicResource ListItemTextStyle}" />
<Label Grid.Row="5" Grid.Column="1" Text="{Binding BudgetedVsActual, StringFormat='({0:+0.0;-0.0})'}" TextColor="{Binding BudgetedVsActual, Converter={StaticResource SignToBrushConverter}}" HorizontalTextAlignment="End"/>
<Label Grid.Row="6" Grid.Column="0" Text="Budgeted vs Suggested:" Style="{DynamicResource ListItemTextStyle}" />
<Label Grid.Row="6" Grid.Column="1" Text="{Binding BudgetedVsSuggested, StringFormat='({0:+0.0;-0.0})'}" TextColor="{Binding BudgetedVsSuggested, Converter={StaticResource SignToBrushConverter}}" HorizontalTextAlignment="End"/>
<Label Grid.Row="7" Grid.Column="0" Text="Budget/Bank Difference:" Style="{DynamicResource ListItemTextStyle}" />
<Label Grid.Row="7" Grid.Column="1" Text="{Binding BankBalanceVariation, StringFormat='({0:+0.0;-0.0})'}" TextColor="{Binding BankBalanceVariation, Converter={StaticResource SignToBrushConverter}}" HorizontalTextAlignment="End"/>

包含转换器资源:

<ContentPage.Resources>
    <ResourceDictionary>
        <common:SignToBrushConverter x:Key="SignToBrushConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

并且绑定到“double”类型的字段。它贯穿于我在这里找到的这个:.

我更改了一些内容以使其针对 Xamarin 表单进行编译:

所以我删除了它们,并且我还将 'Colors.Red' 等更改为 'Color.Red',因为颜色不存在。我相信这应该是 return 笔刷类型?:

public Brush NegativeBrush { get; set; }
public Brush PositiveBrush { get; set; }
public Brush ZeroBrush { get; set; }

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (!IsSupportedType(value))
    {
        return ZeroBrush ?? DefaultZeroBrush;
    }

    double doubleValue = System.Convert.ToDouble(value);

    if (doubleValue < 0d) return NegativeBrush ?? DefaultNegativeBrush;
    if (doubleValue > 0d) return PositiveBrush ?? DefaultPositiveBrush;

    return ZeroBrush ?? DefaultZeroBrush;
}

但随后它抛出一个错误:

System.InvalidCastException Message=Object must implement IConvertible.

但我不确定这里要转换什么,这不只是 returnXamarin 表单的格式化值吗?

我怀疑是因为我没有添加所有 [ValueConversion(typeof(double), typeof(Brush))]

因为我收到此错误:无法找到类型或命名空间名称 'ValueConversionAttribute'(您是否缺少 using 指令或程序集引用?)

我找不到适用于 Xamarin 表单的参考

在Xamarin.Android中,与WPF不同。您不能以某种方式使用相同的代码。

代码在 WPF 中工作。但它在 Xamarin 中不起作用。这就是您收到以下错误的原因。

  DependencyProperty throws "The name 'DependencyProperty' does not exist in the current context"
  DefaultNegativeBrush.Freeze() throws "'Brush' does not contain a definition for 'Freeze'"

根据您提供的代码和link,您可以使用Color来设置标签的TextColor。很容易得到。

    public class SignToBrushConverter : IValueConverter
{
    //public Brush NegativeBrush { get; set; }
    //public Brush PositiveBrush { get; set; }
    //public Brush ZeroBrush { get; set; }
    private static readonly Color DefaultZeroBrush = Color.Green;
    private static readonly Color DefaultNegativeBrush = Color.Red;
    private static readonly Color DefaultPositiveBrush = Color.Green;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (!IsSupportedType(value))
        {
            return DefaultZeroBrush;
        }

        double doubleValue = System.Convert.ToDouble(value);

        if (doubleValue < 0d)
        {
            return DefaultNegativeBrush;
        }
        else if (doubleValue > 0d)
        {
            return DefaultPositiveBrush;
        }

        return DefaultZeroBrush;
    }
    private static bool IsSupportedType(object value)
    {
        return value is int || value is double || value is byte || value is long ||
               value is float || value is uint || value is short || value is sbyte ||
               value is ushort || value is ulong || value is decimal;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

后面的代码:

   public string LastUpdated { get; set; }
    public string NextPayday { get; set; }
    public int AvailableInAccount { get; set; }
    public int OverdrawnExcess { get; set; }
    public int BudgetedVsActual { get; set; }
    public int BudgetedVsSuggested { get; set; }
    public int BankBalanceVariation { get; set; }
    public Page3()
    {
        InitializeComponent();
        LastUpdated = "a";
        NextPayday = "a";
        AvailableInAccount = 1;
        OverdrawnExcess = -2;
        BudgetedVsActual = 111;
        BudgetedVsSuggested = -222;
        BankBalanceVariation = 12;

        BindingContext = this;
    }