Chris Fodor
Chris Fodor

Reputation: 117

Xamarin ImageButton goes invisible after clicked

I have an image button which when I click I want to change its Source (an empty star to a filled one) for the ratings.

My XAML:

<StackLayout Grid.Row="1" Orientation="Horizontal" Spacing="0">
    <ImageButton Source="star_empty.png"
                 HeightRequest="40"
                 WidthRequest="40"
                 VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                 x:Name="star1"
                 BackgroundColor="Transparent"
                 Clicked="ImageButton_Clicked" />

    <ImageButton Source="star_empty.png"
                 HeightRequest="40"
                 WidthRequest="40"
                 VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                 x:Name="star2"
                 BackgroundColor="Transparent"
                 Clicked="ImageButton_Clicked2" />

    <ImageButton Source="star_empty.png"
                 HeightRequest="40"
                 WidthRequest="40"
                 VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                 x:Name="star3"
                 BackgroundColor="Transparent"
                 Clicked="ImageButton_Clicked3" />

    <ImageButton Source="star_empty.png"
                 HeightRequest="40"
                 WidthRequest="40"
                 x:Name="star4"
                 VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                 BackgroundColor="Transparent"
                 Clicked="ImageButton_Clicked4" />

    <ImageButton Source="star_empty.png"
                 HeightRequest="40"
                 WidthRequest="40"
                 x:Name="star5"
                 VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                 BackgroundColor="Transparent"
                 Clicked="ImageButton_Clicked5" />
</StackLayout>

The View class:

private void ImageButton_Clicked(object sender, EventArgs e)
{
    star1.Source = "star_full.png";
    int rating = 1;
}

What could be the issue? The Source does change, it just blinks then goes invisible. I set the isVisible property to true, that doesn't help.

Upvotes: 2

Views: 297

Answers (1)

SushiHangover
SushiHangover

Reputation: 74124

Define the type of ImageSource you are using; file, resource, uri, stream:

Example:

star1.Source = ImageSource.FromResource("star_full.png");

Update:

<StackLayout Grid.Row="1" Orientation="Horizontal" Spacing="0">
        <ImageButton x:Name="star1" Source="star_off.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"/>
        <ImageButton x:Name="star2" Source="star_off.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"/>
        <ImageButton x:Name="star3" Source="star_off.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"/>
        <ImageButton x:Name="star4" Source="star_off.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"/>
        <ImageButton x:Name="star5" Source="star_off.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"/>
</StackLayout>

Code behind:

void ImageButton_Clicked(object sender, System.EventArgs e)
{
    (sender as ImageButton).Source = ImageSource.FromFile("star_on.png");
}

enter image description here

Upvotes: 2

Related Questions