如何在Xamarin.Forms上将图像加载到ImageCell? [英] How to load an image to a ImageCell on Xamarin.Forms?
本文介绍了如何在Xamarin.Forms上将图像加载到ImageCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将图标加载到ListView内的ImageCell中,但是当我构建项目时,该图标没有出现在ImageCell上.
I'm trying to load icons into a ImageCell inside a ListView, but when I build my project the icon doesn't appear on the ImageCell.
这是我的项目结构:
这是我的XAML页面:
Here's my XAML page:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyNotes.StartPage">
<MasterDetailPage.Master Title="Menu">
<ContentPage Title="Menu">
<StackLayout VerticalOptions="FillAndExpand">
<ListView x:Name="listViewMaster" VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<ContentPage>
</ContentPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
这是背后的代码:
using System.Collections.Generic;
using Xamarin.Forms;
namespace MyNotes
{
public partial class StartPage : MasterDetailPage
{
public StartPage ()
{
InitializeComponent ();
List<MasterPageItem> masterPageItems = new List<MasterPageItem>()
{
new MasterPageItem()
{
Title = "Contacts",
IconSource = ImageSource.FromFile(@"c:\Projetos\MyNotes\MyNotes\MyNotes\Resources\image\contacts.png"),
TargetType = typeof(ContactPage)
},
new MasterPageItem()
{
Title = "Todo List",
IconSource = ImageSource.FromFile(@"Resources\image\todo.png"),
TargetType = typeof(TodoListPage)
},
new MasterPageItem()
{
Title = "Reminder",
IconSource = ImageSource.FromResource("Resources.image.reminder.png"),
TargetType = typeof(ContactPage)
}
};
listViewMaster.ItemsSource = masterPageItems;
}
}
}
我在做什么错了?
推荐答案
图像进入您的本地"项目:
The images go into your "native" projects:
-
Xamarin.iOS =
BundledResource
构建类型
Xamarin.Android = AndroidResource
构建类型
Xamarin.Android = AndroidResource
Build Type
IconSource = ImageSource.FromFile("SanFran.jpg")
或使用隐式转换:
IconSource = "SanFran.jpg";
这篇关于如何在Xamarin.Forms上将图像加载到ImageCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文