Xamarin.forms Xamarin表单中带有图像的按钮或ImageButton出现问题
我正在尝试使用一个按钮的图像,而不是文本。我尝试使用带有ImageSource和ImageButton的按钮。两者都不显示图像,仅显示灰色矩形。我直接从ButtonDemos中的ImageButtonDemoPage.XAML复制了XAML,还从文档中复制了用于在按钮上使用图像的XAML。我显示了下面的代码以及结果的屏幕截图。 我在Resources/drawable文件夹以及Resources/drawable hdpi文件夹中有图像,如第二个屏幕截图所示。所有图像的构建操作都设置为AndroidResource。 我对可能出现的问题已经没有什么想法了。任何帮助都将不胜感激。 'Xamarin.forms Xamarin表单中带有图像的按钮或ImageButton出现问题,xamarin.forms,Xamarin.forms,我正在尝试使用一个按钮的图像,而不是文本。我尝试使用带有ImageSource和ImageButton的按钮。两者都不显示图像,仅显示灰色矩形。我直接从ButtonDemos中的ImageButtonDemoPage.XAML复制了XAML,还从文档中复制了用于在按钮上使用图像的XAML。我显示了下面的代码以及结果的屏幕截图。 我在Resources/drawable文件夹以及Resources/drawable hdpi文件夹中有图像,如第二个屏幕截图所示。所有图像的构建操作都设置为Andro
</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>
"
</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,如果可以看到图像显示正确,则可以排除图像问题
</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>
然后我在Xamarin Forms 4.5.0.495中测试它,ImageButton可以正常显示图像,您可以将Xamarin Forms nuget软件包更新到最新版本或4.5.0.495首先,如果它可以正确显示图像,请设置Aspect=Fill in ImageButton。然后,我的Xamarin.Forms nuget包版本是4.5.0.495,它可以正确显示图像。这是我的测试映像和运行结果。我将我的Xamarin.Forms包更新为最新版本,仅此一项就解决了这个问题。我在这个方面使用了各种各样的值,而且看起来都很好。谢谢我将动议这些评论以作答复。请接受它,因为它将帮助其他有类似问题的人。
</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>