Xamarin ImageButton单击后不可见

时间:2019-01-28 19:15:30

标签: xaml xamarin xamarin.forms imagebutton

我有一个图像按钮,当我单击该按钮时,我想更改其来源(评级从空星变为实心星)。

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

View类:

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

可能是什么问题?源确实发生变化,它只是闪烁然后变得不可见。我将isVisible属性设置为true,这没有帮助。

1 个答案:

答案 0 :(得分:2)

定义正在使用的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");
}

enter image description here