嵌入的图像没有显示

Vil*_*ili 8 xamarin.forms

这是便携式项目中的页面

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:b="clr-namespace:Corcav.Behaviors;assembly=Corcav.Behaviors"
                 xmlns:local="clr-namespace:MyMobileApp;assembly=MyMobileApp"
                 x:Class="MyMobileApp.MainPage"
                 x:Name="MainPage">

      <Image Source="{local:ImageResource myimage.jpg}" />
Run Code Online (Sandbox Code Playgroud)

这是我在同一个便携式项目中的ImageResourceExtension

namespace MyMobileApp
{
    [ContentProperty("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
                return null;

            var imageSource = ImageSource.FromResource(Source);

            return imageSource;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图将myimage.jpg添加为嵌入在我的项目的根目录和Resources文件夹中,但是没有显示图像.

在调试时我看到返回的imageSource是Xamarin.Forms.StreamImageSource类型.如何检查是否真的找到了这个?

谁能在这里发现错误?

Vil*_*ili 10

正确的XAML是将应用名称添加到源.

<Image Source="{local:ImageResource MyMobileApp.myimage.jpg}" />
Run Code Online (Sandbox Code Playgroud)


Osa*_*tta 7

默认情况下,图像将具有Build Action:None; 这需要设置为Build Action:EmbeddedResource.右键单击Image> properties> set [Build Action:EmbeddedResource] [在此输入图像描述] 1

  • 即使在使用应用程序名称后,我也无法加载图像。设置构建操作是我必须做的才能让它工作。谢谢奥萨马! (2认同)