我想在点击它时将图像源从white.png更改为blue.png。我试过了,但似乎没有用。
列出ItemsSource模型
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>
似乎图像在列表视图的ViewCell中。
您应该在模型中定义ColorImage。
public class MyModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string imagePath;
public string ColorImage
{
get { return imagePath; }
set
{
imagePath = value;
NotifyPropertyChanged("ColorImage");
}
}
//...other properties
public MyModel()
{
ColorImage = "white.png";
}
}
private void AddImg(object obj)
{
var model = obj as MyModel;
model.ColorImage = "imgcolorblue.png";
}
您为参数“ ColorImage”使用了错误的类型。图像的来源是ImageSource类型。
假设您在项目上正确设置了图像,以下代码应该可以解决您的问题
private void AddImg(object obj)
{
ColorImage = ImageSource.FromFile("imgcolorblue.png");
}
也通过ImageSource更改“ ColorImage”的类型。