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

Xaml 如何在Xamarin.Forms中添加图像和图标?,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,这是我第一次使用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="hell

这是我第一次使用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>

我想把它加在这里
我想把它加在这里

您可以设置页面的图标,但我不知道有图标控件。可以指向本地资源或联机资源来设置页面的图标

<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" />

Xamarin.Forms
中,建议在本机项目中添加图像,因为每个平台需要不同的尺寸和分辨率图像。因此,我们在这里添加了我们如何为Android和iOS项目所做的图片

Android:

  • 可拉伸mdpi
  • 可拉伸hdpi
  • 可拉伸xhdpi
  • 可拉伸xxhdpi
hdpi
mdpi
可拉伸xhdpi
可拉伸xxhdpi
指屏幕密度,单位为每英寸点数

最佳做法是将应用程序启动程序图标放置在mipmap(而不是 可绘制(文件夹)

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

将图像添加到
Drawable
文件夹中:

iOS:

这种在iOS应用程序中处理图像的方法自iOS 9以来就遭到了苹果的反对。您应该改用资产目录。

有关更多信息,请参阅。

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

请记住,在iOS中,所有图像都位于同一文件夹中,但命名约定不同

资源
文件夹中添加图像:

UWP:

通用Windows平台(UWP)-使用Build Action:Content将映像放置在应用程序的根目录中

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

在共享项目中使用图像

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


作为一个旁注,你可以考虑使用SVG的图标(签出链接):我如何改变图像的宽度和高度?你可以使用HeaveTrestQuestand WIDRESQuestRESP来确定高度和宽度Y。在我可以用“代码>图标”=“图标.pNG”< /C>或<代码>源>之前,我需要做什么?“waterfront.jpg”
。能否添加有关如何添加UWP图像以完成的信息?