【问题标题】:I cannot get the embedded images to display in (Xamarin Forms Application)我无法在(Xamarin 表单应用程序)中显示嵌入的图像
【发布时间】:2017-10-11 05:27:10
【问题描述】:

我正在尝试使用 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 图片)

要将字符串值转换为图像类型,我使用 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);

    }

这是配方类的 Xaml 文件 RecipiePage.xaml 这些是我用来显示 RecipePage 的所有页面,但图像没有显示,我也没有收到任何错误消息。如果有人可以提供帮助,我已经花费了很多时间。谢谢

【问题讨论】:

    标签: c# xaml xamarin.forms visual-studio-2017


    【解决方案1】:

    首先确保您确实在 Visual Studio 中将图像的 Build Action 更改为 EmbededResource。

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

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

    您可以通过查看其属性来确认该图像的 Resource Id 是什么。

    更多嵌入图片可见here

    【讨论】:

    • 非常感谢@apineda 的回答。如果您阅读我提供的 Recipie Page.Xaml 文件,我添加了程序集名称并将其路径设置为 Images 文件夹。我还对嵌入式资源进行了图像构建操作。但是图像不显示我尝试了另一个项目的解决方案,该项目具有非常相似的文件和图像,但该项目中没有显示。
    • 我认为问题出在文件结构上,图像文件夹没有被正确调用或者我有错误的程序集
    • @oopragmmer 您尝试(并工作)的另一个项目也是共享项目吗?
    • 所以我相信问题可能就在那里.. 共享项目结构......不同.. 它们实际上不是一个项目,因为它们不构建,所以更像是一种“链接”文件到不同的项目,但文件本身不是共享项目的一部分,而是另一个项目的一部分。在这种情况下,从 Android 运行时,您的路径应指向 Android 命名空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2018-09-05
    相关资源
    最近更新 更多