如何在Xamarin.Forms中添加图像和图标?

4

这是我第一次使用Xamarin.Forms,我想知道如何添加图标或图片。我试图查找相关信息,但发现很复杂。


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:hello"
             x:Class="hello.MainPage">
    <StackLayout >
        <Label Text="Hello" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" 
               TextColor="White"
               FontSize="30"
               FontAttributes="Italic"/>

        <Image /> I am trying to add it here
        <Icon/> I am trying to add it here

        <Label Text="Name" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" 
               TextColor="White"
               Font="large"/>
        <entry placeholder="Enter your name here"/>
    </StackLayout>
</ContentPage>

2
https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/ - Jason
1
顺便提一下,您可以考虑使用SVG作为图标(请查看链接):https://github.com/luberda-molinet/FFImageLoading/tree/master/samples/ImageLoading.Forms.Sample/Shared - Mathias Kirkegaard
2个回答

5
Xamarin.Forms中,建议仅在本地项目中添加图像,因为每个平台需要不同尺寸和分辨率的图像。因此,我们将介绍如何添加Android和iOS项目中的图像。 Android:
  • drawable-mdpi
  • drawable-hdpi
  • drawable-xhdpi
  • drawable-xxhdpi

hdpimdpidrawable-xhdpidrawable-xxhdpi是指屏幕密度(每英寸点数)。

最佳实践是将应用程序启动器图标放置在mipmap中(而不是drawable文件夹中)

请记住,在Android中,所有图像都在不同的文件夹中,但具有相同的命名约定。

Drawable文件夹中添加图像:

enter image description here

iOS:

自iOS 9以来,苹果已经废弃了在iOS应用程序中使用图像的这种方法。您应该改用Asset Catalog

更多信息,请参见Image Sizes and Filenames

对于iOS项目,请展开Resources文件夹并在此处添加您的图像。在iOS中,我们遵循一种命名约定来区分我们的图像。 如果您的图像名称为abc.png,则对于该图像的高版本,可以使用abc@2x.pngabc@3x.png等。

请记住,在iOS中,所有图像都在同一个文件夹中,但具有不同的命名约定。

Resource文件夹中添加图像:

enter image description here

UWP:

通用Windows平台(UWP)-将图像放置在应用程序的根目录中,构建操作:内容。

UWP图像文件名可以在文件扩展名之前加上.scale-xxx后缀,其中xxx是应用于该资产的缩放百分比,例如myimage.scale-200.png。然后可以在代码或XAML中引用图像,而无需缩放修饰符,例如只是myimage.png。平台将根据显示器的当前DPI选择最近的适当资产比例。

在共享项目中使用图片

<Image  Source="Image1"  WidthRequest="80" HeightRequest="80" ></Image>

你能否添加有关如何添加UWP图像以进行完成的信息? - Christian

1
您可以为页面设置图标,但我不知道有图标控件。为页面设置图标可以指向本地资源或在线资源。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     xmlns:local="clr-namespace:hello"
     x:Class="hello.MainPage" 
     Icon="Icon.png">

设置显示一个图像,您需要设置图像的源

<Image Source="waterfront.jpg" />

如何更改图像的宽度和高度? - user8834153
1
查看显示图像 - Shawn Kendrot
您可以使用HeightRequest和WidthRequest属性来确定高度和宽度y。 - cyberfingaz2007
在我可以使用 Icon="Icon.png"Source="waterfront.jpg" 指向图像之前,我需要做什么? - Christian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接