当我使用Commands时,按钮在bug上烦扰并且其中的标签消失了

时间:2019-06-06 08:59:56

标签: c# xaml xamarin.forms

我正在使用一些命令使我的按钮更具动态性,并且我有一些错误。其中之一是,当我对程序进行启动时,我的按钮样式错误,看起来像这样:

sample

代替此:

correct

仅当调用该命令的代码后面时才显示正确的样式。

第二个问题是我的文本没有出现。

我的按钮代码:

<telerikPrimitives:RadBorder x:Name="BorderMinus" BorderColor="#3F3C42" BorderThickness="1" Grid.Column="0" Grid.Row="1" Margin="3,2,2,2">
   <Button x:Name="QntMinus" Text="-" Padding="0" Command="{Binding DecrementQuantityCommand, Source={x:Reference ExtendedContentView}}" Margin="-1" BackgroundColor="{Binding DecrementQuantityCommandEnabled, Source={x:Reference ExtendedContentView}, Converter={StaticResource Batata}, ConverterParameter={x:Reference Name=MainColorGrid2}}"/>
</telerikPrimitives:RadBorder>

<Label x:Name="QntLabel" Text="{Binding SelectedSize.Quantity, Source={x:Reference ExtendedContentView}}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="White" Grid.Column="1" Grid.Row="1"/>

<telerikPrimitives:RadBorder x:Name="BorderPlus" BorderColor="#FF8A00" BorderThickness="1" Grid.Column="2" Grid.Row="1" Margin="2,2,3,2">
   <Button x:Name="QntPlus" Text="+" Padding="0" Command="{Binding IncrementQuantityCommand, Source={x:Reference ExtendedContentView}}" Margin="-1" BackgroundColor="{Binding IncrementQuantityCommandEnabled, Source={x:Reference ExtendedContentView}, Converter={StaticResource Batata}, ConverterParameter={x:Reference Name=MainColorGrid2}}"/>
</telerikPrimitives:RadBorder>

Im使用的掩体是在xaml中实现的,如下所示:

<ContentView.Resources>
   <ResourceDictionary>
      <local:IsActiveToColorConverter x:Key="Batata" />
   </ResourceDictionary>
</ContentView.Resources>

我的隐藏代码:

public partial class ExtendedButton : ContentView
{
   public bool IncrementQuantityCommandEnabled { get; set; }
   public bool DecrementQuantityCommandEnabled { get; set; }
   public ICommand IncrementQuantityCommand { get; set; }
   public ICommand DecrementQuantityCommand { get; set; }

   public void IncrementQuantity()
   {           
      if (SelectedSize.IncrementQuantity())
      {
         if (SelectedSize.Quantity == 10)
         {
            IncrementQuantityCommandEnabled = false;
         }

         DecrementQuantityCommandEnabled = true;
      }            
   }

   public void DecrementQuantity()
   {
      if (SelectedSize.DecrementQuantity())
      {
         if (SelectedSize.Quantity == 1)
         {
            DecrementQuantityCommandEnabled = false;
         }

         IncrementQuantityCommandEnabled = true;
      }
   }

   public ExtendedButton()
   {
      InitializeComponent();

      IncrementQuantityCommandEnabled = true;
      DecrementQuantityCommandEnabled = true;
      IncrementQuantityCommand = new Command(() => IncrementQuantity(), () => IncrementQuantityCommandEnabled);
      DecrementQuantityCommand = new Command(() => DecrementQuantity(), () => DecrementQuantityCommandEnabled);
   }
}

public class IsActiveToColorConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {
      Grid grid = parameter as Grid;
      Color mainColorDisabled = Color.Green;
      Color mainColor = Color.Pink;

      bool first = true;

      foreach (var item in grid.Children)
      {
         var label = item as Label;

         if (first)
         {
            mainColor = label.BackgroundColor;
            first = false;
         }
         else
         {
            mainColorDisabled = label.BackgroundColor;
         }
      }

      var isActive = (bool)value;

      if (isActive)
      {
         return mainColor;
      }
      else
      {
         return mainColorDisabled;
      }
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
      throw new NotImplementedException();
   }
}

0 个答案:

没有答案