簡體   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