簡體   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