布尔到图像转换器不起作用。图像不变

时间:2019-09-20 12:41:41

标签: c# mvvm xamarin.forms

我已经有一个运行中的bool到图像转换器,可以在另一个应用程序上完美运行。我已经复制并修改了代码,但无法正常工作。你们有个主意吗?

我的XAML:

    <Grid>
        <Grid.Resources>
            <ResourceDictionary>
                <services:BooleanToObjectConverterService x:Key="boolToStyleImage"
                              x:TypeArguments="Style">
                    <services:BooleanToObjectConverterService.FalseObject>
                        <Style TargetType="Image">
                            <Setter Property="Source" Value="container_noconnect.png" />
                        </Style>
                    </services:BooleanToObjectConverterService.FalseObject>
                    <services:BooleanToObjectConverterService.TrueObject>
                        <Style TargetType="Image">
                            <Setter Property="Source" Value="container_withconnect.png" />
                        </Style>
                    </services:BooleanToObjectConverterService.TrueObject>
                </services:BooleanToObjectConverterService>
            </ResourceDictionary>
        </Grid.Resources>
        <Image Style="{Binding HasWebserviceConnection, Converter={StaticResource boolToStyleImage}}" Aspect="AspectFill"/>
    </Grid>

这些是我的课程: BooleanToObjectConverterService:

    public class BooleanToObjectConverterService<T> : IValueConverter
    {
        public T FalseObject { set; get; }

        public T TrueObject { set; get; }

        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            return (bool)value ? this.TrueObject : this.FalseObject;
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            return ((T)value).Equals(this.TrueObject);
        }
    }

ImageResourceExtension:

    [ContentProperty("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
                return null;
            var imageSource = ImageSource.FromResource(Source);
            return imageSource;
        }
    }

我已经调试了代码,我的HasWebserviceConnection正确地更改了值。

编辑: 感谢@Janne Matikainen,我使用了DataTriggers,现在可以使用了。我明天要更新解决方案。 谢谢

1 个答案:

答案 0 :(得分:0)

就这么简单。谢谢@Janne Matikainen。

<Image x:Name="image" Source="container_noconnect.png" Aspect="AspectFill" >
    <Image.Triggers>
        <DataTrigger TargetType="Image" Binding="{Binding Source={x:Reference advanceButton}, IsEnabled}" Value="True">
            <Setter Property="Source" Value="container_withconnect" />
        </DataTrigger>
    </Image.Triggers>
</Image>