在ios的框架背景颜色在xamarin形式不运作

时间:2017-07-25 09:25:31

标签: xamarin xamarin.ios xamarin.android xamarin.forms frame

通过转换器为ios和android应用帧背景颜色。 对于Android它工作正常。但是对于ios不能正常工作。

请找到以下Xaml代码。

<ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <Frame Margin="0,5"
                       Padding="10"
                       CornerRadius="5"
                       BackgroundColor="{Binding IsSelected, Converter={StaticResource boolToColor}}">
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*" />
                      <ColumnDefinition Width="32" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding Title}"
                           FontSize="Medium"
                           TextColor="White"
                           Style="{StaticResource lblSecondaryStyle}" />
                    <Image Grid.Column="1"
                           Source="{Binding IsSelected, Converter={StaticResource boolToImage}}"
                           HeightRequest="30"
                           WidthRequest="30"
                           Aspect="AspectFit"
                           HorizontalOptions="End" />
                  </Grid>
                </Frame>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>

下面是我的boolToColorConverter逻辑。

public class BoolToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return value;
            }
            if (System.Convert.ToBoolean(value))
            {
                return (Color)Application.Current.Resources["primary"];
            }
            return (Color)Application.Current.Resources["Secondary_blue_background"];
        }

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

注意: - 在我的Xaml代码中,我还使用了boolToImageConverter,它绑定到同一个属性IsSelected,令我惊讶的是它正在按预期工作。

请指导我在哪里做错了。

1 个答案:

答案 0 :(得分:0)

您尝试以下解决方案,这肯定会对您有用。

<DataTemplate>
            <ViewCell>
              <ViewCell.View>
              <Grid>
                <Frame Margin="0,5"
                       Padding="10"
                       CornerRadius="5"
                       IsVisible="{Binding IsSelected}"
                       BackgroundColor="{StaticResource TrueColorHere}">
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*" />
                      <ColumnDefinition Width="32" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding Title}"
                           FontSize="Medium"
                           TextColor="White"
                           Style="{StaticResource lblSecondaryStyle}" />
                    <Image Grid.Column="1"
                           Source="{Binding IsSelected, Converter={StaticResource boolToImage}}"
                           HeightRequest="30"
                           WidthRequest="30"
                           Aspect="AspectFit"
                           HorizontalOptions="End" />
                  </Grid>
                </Frame>

                <Frame Margin="0,5"
                       Padding="10"
                       CornerRadius="5"
                       IsVisible="{Binding IsSelected, Converter={StaticResource inverseBool}}"
                       BackgroundColor="{StaticResource FalseColorHere}">
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*" />
                      <ColumnDefinition Width="32" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding Title}"
                           FontSize="Medium"
                           TextColor="White"
                           Style="{StaticResource lblSecondaryStyle}" />
                    <Image Grid.Column="1"
                           Source="{Binding IsSelected, Converter={StaticResource boolToImage}}"
                           HeightRequest="30"
                           WidthRequest="30"
                           Aspect="AspectFit"
                           HorizontalOptions="End" />
                  </Grid>
                </Frame>
              </Grid> 
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>

享受编码:)