xaml - Xamarin ImageButton 单击后不可见

标签 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,但这没有帮助。

最佳答案

定义您正在使用的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

关于xaml - Xamarin ImageButton 单击后不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54408807/

相关文章:

xamarin - 如何将 Azure 移动应用服务与现有 SQL 数据库结合使用

android - 在 View 模型之间传递数据 MvvmCross

ios - Xamarin 表单电话无法在 iOS 上运行

xamarin.forms - 如何用 F# 声明 Xamarin.Forms.Dependency?

.net - WPF 数据网格行删除线

silverlight - 创建 HUE 颜色条

XamlParseException 无法分配给属性。绑定(bind)不适用于附加属性

c# - ObservableCollection 以及存储库

android - Xamarin Android 中的 java.lang.NoClassDefFoundError : android. support.v7.app.AppCompatDelegateImplV14

xaml - 如何在 xamarin.forms 应用程序中添加 url