繁体   English   中英

我无法在(Xamarin Forms应用程序)中显示嵌入的图像

[英]I cannot get the embedded images to display in (Xamarin Forms Application)

我正在尝试使用Visual Studio 2017在Xamarin Froms中创建一个非常简单的跨平台应用程序。该应用程序的目的是在小型GridView中显示食物食谱,并带有食谱名称及其图片。

我使用以下静态类文件为配方添加数据。 RecipeData.cs

**// Class that adds  Data to Recipe Items**
public static class RecipeData
{
  static List<Recipe> _allRecipes;

    public static List<Recipe> AllRecipes
    {
        //List<Recipe> _allRecipes = new List<Recipe>();
        get
        {

            if(_allRecipes == null)
            {
                _allRecipes = new List<Recipe>
                {
                    new Recipie
                    {
                        RecipeName="Eggs Benedict",
                        CookTime = "24 min",
                        PreprationTime = "5 min",
                        NumberOfServing = 8,
                        WillMakeAgain = true,
                        MealType = MealType.Breakfast,
                        Difficulty = Difficulty.Easy,
                        Directions="1. Fill a large saucepan with about 4 inches of water, add vinegar, and bring to a boil. Fill a shallow dish or pie plate with warm water. Reduce heat under saucepan to medium, so water is just barely simmering. Break 1 egg at a time into a small heat-proof bowl. Gently tip bowl into water; carefully slide egg into water. Repeat with remaining eggs.\n\n2. When eggs begin to become opaque, remove them from the saucepan with a slotted spoon in the order in which they were added. Transfer the eggs to the dish of warm water. This process should take about 3 minutes.\n\n3. Prepare the hollandaise sauce, and set aside, keeping it warm.\n\n4. Heat a medium skillet over medium heat. Add Canadian bacon, and cook until well browned on both sides. Divide bacon among the English-muffin halves. For each serving, use a slotted spoon to remove one egg from warm water; set spoon and egg briefly on a clean cloth or paper towel to drain. Gently place the egg on a bacon-topped muffin, and spoon the reserved warm hollandaise sauce over the top.",
                        Ingredients="1 tablespoon white vinegar" + Environment.NewLine +
                            "8 large eggs\nHollandaise Sauce" + Environment.NewLine +
                            "1/2 pound (16 slices) Canadian bacon" + Environment.NewLine +
                            "4 English muffins, split in half, toasted",
                        ImageName = "Muffin.jpg"
                    },

这是我的项目的外观文件看起来像:

我当前的项目文件的屏幕截图(显示我有Muffin.jpg图片

我当前的项目文件的屏幕截图(显示我有Muffin.jpg图片)

要将字符串值转换为图像类型,我正在使用ImageBindingConver类

class ImageBindingConverter : IValueConverter
{
    public string Assembly { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var source = value as string;
        if (string.IsNullOrEmpty(source))
            return null;

        var imagePath = $"{Assembly}.{source}";

        return ImageSource.FromResource(imagePath);

    }

这是食谱类 RecipiePage.xaml 的Xaml文件,这些是我用来显示RecipePage的所有页面,但是图像不显示,并且我也没有收到任何错误消息。 如果有人可以帮助我,我一直在花费很多时间,那就太好了。 谢谢

首先,请确保在Visual Studio中确实将图像的“生成操作”更改为EmbededResource。

接下来,您需要更改图像路径。 默认情况下,VS或XS将使用点“。”连接。 图像的程序集名称+任何文件夹所在的文件夹+带有扩展名的图像名称,在您的情况下,图像名称应为:

var imagePath = $"{Assembly}.Images.{source}";

您可以查看其属性,确认该图像的资源ID是什么。

在这里可以看到更多的嵌入式图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM