如何在Xamarin.Forms上将图像加载到ImageCell?

时间:2016-05-27 18:01:21

标签: xamarin xamarin.forms

我试图将图标加载到ListView内的ImageCell中,但是当我构建项目时,图标不会出现在ImageCell上。

这是我的项目结构:

enter image description here

这是我的XAML页面:

<?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;
        }
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:4)

图像进入“原生”项目:

  • Xamarin.iOS = BundledResource构建类型

  • Xamarin.Android = AndroidResource构建类型

IconSource = ImageSource.FromFile("SanFran.jpg")

或使用隐式转换:

IconSource = "SanFran.jpg";

enter image description here

Working with Images

答案 1 :(得分:2)

您是否已将添加到项目中的图像标记为嵌入式重建?

embedded resource