繁体   English   中英

Xamarin Forms:尝试为 Android 构建条件格式,错误: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”类型的字段。 它贯穿了我在此处找到的内容: String format positive and negative values and conditions color format XAML

我更改了一些内容以使其编译为 Xamarin 表单:

  • DependencyProperty 抛出“当前上下文中不存在名称‘DependencyProperty’”
  • DefaultNegativeBrush.Freeze() 抛出“'Brush' 不包含'Freeze' 的定义”

所以我删除了它们,我还将“Colors.Red”等更改为“Color.Red”,因为颜色不存在。 我相信这应该返回画笔类型?:

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.

但我不确定这里会投射什么,这不就是将格式值返回给 Xamarin 表单吗?

我怀疑这是因为我没有添加所有[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'"

根据您提供的代码和链接,您可以使用 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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM