从项目文件加载图像 [英] Loading image from projects files

查看:148
本文介绍了从项目文件加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取png图像,这是我的资源文件夹。我测试了这里编写的解决方案:将图像添加到ListBox(c#,windows电话7)。首先,我想为ListBox中的每个项目获取相同的图像。但我无法实现这一点。图片没有显示。

I'm trying to get png image which is my Resource folder. I tested solution that was written here :Add images to ListBox (c#, windows phone 7). For start I wanted to get the same image for every item in my ListBox. But I can't achive that. The picture doesn't show.

这是我在xaml中的列表的样子:

It's how my list in xaml look like:

<ListBox x:Name="ProductList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <Image Source="{Binding ImagePath}"  Stretch="None"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

这就是我填充物品的方式:

This is how I populate Items:

List<ImageData1> productsList = new List<ImageData1>();

foreach (ProductItem item in ProductsTable)
{
    if (item.Category == chosenCategory.TrId)
    {
            ImageData1 img = new ImageData1();
            img.Name = item.Name;
            img.ImagePath = new Uri("Resources/img.png", UriKind.RelativeOrAbsolute);
            productsList.Add(img);
    }
}

ProductList.ItemsSource = productsList;

这是我的班级来保存图片数据:

Here is my class to hold image data:

public class ImageData1
{
    public Uri ImagePath
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}


推荐答案

尝试在图像路径之前放置 / 。喜欢 /Resources/img.png 。并尝试将 UriKind.RelativeOrAbsolute 更改为 UriKind.Relative 。并确保您的图像添加为内容构建操作图像属性)。这样它对我有用。

Try putting / before your image path. Like /Resources/img.png. And try to change UriKind.RelativeOrAbsolute to UriKind.Relative. And make sure your images are added as Content (Build Action property of image). That way it works for me.

这篇关于从项目文件加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆