【问题标题】:Issue with Button with Image or ImageButton in Xamarin FormsXamarin 表单中带有 Image 或 ImageButton 的按钮问题
【发布时间】:2020-05-25 14:57:02
【问题描述】:

我正在尝试使用带有图像而不是文本的按钮。我尝试使用带有 ImageSource 和 ImageButton 的 Button。既不显示图像,也只显示一个灰色矩形。我直接从 ButtonDemos 中的 ImageButtonDemoPage.xaml 复制了 XAML,还从文档中复制了用于在 Button 上使用图像的 XAML。我展示了下面的代码以及结果的屏幕截图。 我在 Resources/drawable 文件夹以及 Resources/drawable-hdpi 文件夹中有图像,如第二个屏幕截图所示。所有图像的构建操作都设置为 AndroidResource。 我已经没有什么想法可能出问题了。任何帮助,将不胜感激。 '''

            </controls:UpcomingPaymentsView>
            <ScrollView HorizontalOptions="CenterAndExpand">
                <!--<Button>
                    <Button.ImageSource>
                        <OnPlatform x:TypeArguments="ImageSource">
                            <On Platform="iOS, Android" Value="MonkeyFace.png" />
                            <On Platform="UWP" Value="Assets/MonkeyFace.png" />
                        </OnPlatform>
                    </Button.ImageSource>
                </Button>-->
                <ImageButton Source="MonkeyFace.png" 
                    WidthRequest="72"
                    HeightRequest="72"
                    FlexLayout.AlignSelf="Center"
                    FlexLayout.Grow="1"
                    Aspect="AspectFit">
                </ImageButton>
            </ScrollView>
        </StackLayout>

'''

【问题讨论】:

  • 首先,请在ImageButton中设置Aspect="Fill",如果它可以正确显示图像。然后,我的Xamarin.Forms nuget 包版本是 4.5.0.495,它可以正确显示图像。这是我的测试图像和运行结果。 imgur.com/a/9edlgE2
  • 我将 Xamarin.Forms 包更新到最新版本,仅此一项似乎就解决了问题。我已经为 Aspect 使用了各种值,并且似乎一切正常。谢谢!
  • 我会移动cmets来回答。请接受它作为答案,它将帮助其他有类似问题的人。

标签: xamarin.forms


【解决方案1】:

首先,请设置ImageButton的Aspect=Fill,如果可以看到图片显示正确,则可以排除图片问题。

然后我在 Xamarin Forms 4.5.0.495 中测试它,ImageButton 可以正常显示图像,您可以将 Xamarin Forms nuget 包更新到最新或 4.5.0.495

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多