本地图片
图片可以通过共享代码引用. 引用的图片必须同时存在不同平台之中, 并且图片名称符合不同平台的命名标准 (例如安卓.仅仅小写, 数字等).虽然Xamarin引用图片的地址相同,但各平台资源存放的地址不相同,同时资源的生成方式也有区别(右键-属性-生成-?)。
- iOS -把图片放置于 Resources文件夹内, 属性内,生成操作选择: BundleResource . Retina versions of the image should also be supplied - two and three times the resolution with a @2x or @3x suffixes on the filename before the file extension (eg. myimage@2x.png).
- Android - 把图片放置于 Resources/drawable 目录,属性内生成操作选择: AndroidResource. High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi ,drawable-hdpi , and drawable-xhdpi ).
-
Windows Phone- Place images in the application's root directory withBuild Action: Content. - Windows/UWP - 把图片放置在根目录中, 生成操作: 内容 .
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WorkingWithImages.LocalImagesXaml"> <StackLayout VerticalOptions="Center" HorizontalOptions="Center"> <Label Text="ImageSource.FromFile" /> <Image Source="waterfront.jpg" /> <Label Text="'waterfront.jpg' is referenced in Xaml" /> </StackLayout> </ContentPage>
本地图片放置和生成配置正确图片才能正常显示。
更多官方资料:https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/