ListView中的Xamarin OnImageClick更改图像颜色

问题描述 投票:0回答:1

我有问题。我创建了一个带有网格的ListView。在网格中,我有一个图像,但是现在我希望能够单击该图像,完成后,该图像应更改颜色。这是我的ListView的axml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="MyApp.HomePage">
    <ContentPage.Content>
        <ListView x:Name="ListViewMain" VerticalOptions="FillAndExpand" BackgroundColor="#212121" SelectionMode="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid x:Name="GridMain">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40" x:Name="Row0_Height"/>
                                <RowDefinition Height="180" x:Name="Row1_Height"/>
                                <RowDefinition Height="180" x:Name="Row2_Height"/>
                                <RowDefinition Height="40" x:Name="Row3_Height"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="40" x:Name="Column0_Width" />
                                <ColumnDefinition Width="*" x:Name="Column1_Width" />
                                <ColumnDefinition Width="40" x:Name="Column2_Width" />
                            </Grid.ColumnDefinitions>
                            <Label Text="{Binding Creator}" TextColor="White" FontSize="Body" FontAttributes="Bold" Grid.Column="1" Grid.Row="0" VerticalOptions="Center" HorizontalOptions="Start"/>
                            <Image Source="VoteUp.png" VerticalOptions="End" HorizontalOptions="Center" Grid.Row="1" Grid.Column="0"/>
                            <Image Source="VoteDown.png" VerticalOptions="Start" HorizontalOptions="Center" Grid.Row="2" Grid.Column="0"/>
                            <Image Source="{Binding ImageLocation}" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="2" BackgroundColor="AliceBlue" VerticalOptions="Fill" HorizontalOptions="Fill"/>
                            <Image Source="Favorite.png" Grid.Row="3" Grid.Column="1" HorizontalOptions="Start"/>
                            <Image Source="Send_Dark.png" Grid.Row="3" Grid.Column="1" HorizontalOptions="End"/>
                            <Image Source="Save_Dark.png" Grid.Row="3" Grid.Column="2" HorizontalOptions="End"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

例如,以Save_Dark.png为例,如何在该图像上设置点击侦听器,然后更改颜色?

c# xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票

使用手势识别器

<Image Source="Save_Dark.png" Grid.Row="3" Grid.Column="2" HorizontalOptions="End" >
  <Image.GestureRecognizers>
    <TapGestureRecognizer Tapped="Image_Tapped" />
  </Image.GestureRecognizers>
</Image>

protected void Image_Tapped(object sender, EventArgs args)
{
  var i = (Image)sender;
  i.Source = ... new image source here ...
}                    
© www.soinside.com 2019 - 2024. All rights reserved.