【发布时间】:2020-06-03 20:57:02
【问题描述】:
我想在点击时将图像源从 white.png 更改为 blue.png ,当图像更改为 white.png 并且当它是 blue.png 时,我需要访问每个commannd ,也.it会根据图像结果将其设置为 true 或 false 会有所帮助。
所有这些都将出现在一个列表中。我需要访问每个被点击的人。
我试过了:
IValueConveter
public class ConverterAddRemoveImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool isAddedToCart = (bool)value;
if (isAddedToCart)
{
return "FilledIcon"; //This will be a string
}
else
{
return "EmptyIcon"; //This will be a string
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Xaml
<Image Source="{Binding IsAddedToCart, Converter={StaticResource AddRemoveImage}}"/>
这只是向我展示 else ,永远不会成为真的。理想情况下,我想访问它们中的每一个并为其添加逻辑,并且显然会更改每次点击时的图像。
第二种方法 - 这里的问题是 - 只被点击一次,我无法在第二次点击时返回到前一个图像,而且,我不知道如何访问每个命令。 (我想模仿一个开关打开和关闭,然后他们每个人都做一些事情)
public ImageSource ColorImage { get; set; }
查看模型
public ICommand SwitchIMGCommand
{
get;
set;
}
private void AddImg(object obj)
{
var selection = obj as ExistingModel;
selection.ColorImage = "FilledIcon.png";**
ColorImage= "FilledIcon.png";**I tried this and it doesnt change
to this img
}
private ImageSource imagePath = "white.png";
public ImageSource ColorImage
{
get { return imagePath; }
set
{
imagePath = value;
PropertyChanged(this, new PropertyChangedEventArgs("ColorImage"));
}
}
构造函数
SwitchIMGCommand = new Command(AddImg);
XAML
<Image
Source="{Binding ColorImage}">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Source={x:Reference listView}, Path=BindingContext.SwitchIMGCommand}" CommandParameter="{Binding .} "
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
【问题讨论】:
-
我已将切换功能添加到 Lucas anwser
标签: image xaml xamarin.forms data-binding inotifypropertychanged