【问题标题】:How to load an image to a ImageCell on Xamarin.Forms?如何将图像加载到 Xamarin.Forms 上的 ImageCell?
【发布时间】:2016-05-28 02:19:08
【问题描述】:

我正在尝试将图标加载到 ListView 内的 ImageCell 中,但是当我构建我的项目时,该图标没有出现在 ImageCell 上。

这是我的项目结构:

这是我的 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;
        }
    }
}

我做错了什么?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    图像进入您的“原生”项目:

    • Xamarin.iOS = BundledResource 构建类型

    • Xamarin.Android = AndroidResource 构建类型


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

    或使用隐式转换:

    IconSource = "SanFran.jpg";
    

    Working with Images

    【讨论】:

    • 谢谢。一旦我将图像移动到本机项目中的正确文件夹并设置正确的构建操作,它就可以正常工作了。
    • 我看到了与 ImageCell 类似的东西,我尝试了所有我能想象的变体,但它没有出现。也许我还需要设置其他东西....有什么想法吗?
    • @TheSenator 我建议您使用相关代码、图像设置发布一个新问题,以了解您在做什么。
    【解决方案2】:

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

    embedded resource

    【讨论】:

    • 我认为您需要将图像添加为 ImageSource.FromResource 而不是 ImageSource.FromFile。
    • @AviK。仅供参考:将它们嵌入到程序集中并使用FromResource 是一个可行的选项,但在性能和资源消耗方面不建议这样做。最佳实践方式是使用原生的BundledResource 或AndroidResource 构建类型。
    • @SushiHangover 谢谢。我在这里通过问题创建stackoverflow.com/questions/40771645/…
    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多