【问题标题】:Xamarin ImageButton goes invisible after clicked单击后 Xamarin ImageButton 变得不可见
【发布时间】:2019-06-21 20:35:24
【问题描述】:

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

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

视图类:

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

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

【问题讨论】:

    标签: xaml xamarin xamarin.forms imagebutton


    【解决方案1】:

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

    【讨论】:

    • 当我为第 5 颗星编写代码时,第 5 颗(点击)变得不可见,但前 4 颗很好,在定义 FromResource 时,它们都变得不可见,FromFile 也一样作为我的代码。
    • @ChrisFodor 您使用的是什么类型的图像? (嵌入式资源?基于 Android 资源的 Drawable 等...?)
    • Drawables文件夹中的Android资源
    • 您确定图像按钮内容在图像切换后可见,请尝试将VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 添加到您的 ImageButton XAML 作为测试(在您的情况下,FromFile 是正确的)
    • 谢谢,原来 WidthRequest 和 HeightRequest 是问题所在。
    猜你喜欢
    • 2015-07-08
    • 1970-01-01
    • 2019-11-17
    • 2015-05-04
    • 2021-03-24
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多