单击后 Xamarin ImageButton 不可见

Xamarin ImageButton goes invisible after clicked

我有一个图像按钮,当我点击它时,我想更改其收视率来源(空星变为实心星)。

我的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>

观点class:

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

可能是什么问题?源头确实改变了,它只是闪烁然后消失了。我将 isVisible 属性 设置为 true,这没有用。

定义您使用的ImageSource类型;文件、资源、uri、流:

示例:

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

更新:

<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>

后面的代码:

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