我有一个图像按钮,当我单击该按钮时,我想更改其评级来源(空星变为实星)。
我的 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,但这没有帮助。
最佳答案
定义您正在使用的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");
}
关于xaml - Xamarin ImageButton 单击后不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54408807/