C# 未显示图像源

C# 未显示图像源,c#,xamarin.forms,C#,Xamarin.forms,你好,我已经创建了一个xamarin跨平台应用程序 我为每个iOS和Android平台设置了一个图像。 如果我点击这个图像,它应该从资源更改为另一个图像 项目: Mainpage.xaml <Frame BorderColor="Teal" BackgroundColor="White"> <Grid> <Grid.ColumnDefinitions> <Colum

你好,我已经创建了一个xamarin跨平台应用程序

我为每个iOS和Android平台设置了一个图像。 如果我点击这个图像,它应该从资源更改为另一个图像

项目:

Mainpage.xaml

<Frame BorderColor="Teal" BackgroundColor="White">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition 
                Width="100"
            />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition 
                Height="100"
            />
        </Grid.RowDefinitions>

        <Image
            Grid.Column="0"
            Grid.Row="0"
            Aspect="AspectFill"
            x:Name="imgEnglisch"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"
            BackgroundColor="White">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="IbtnEnglisch_Clicked" NumberOfTapsRequired="1"/>
                </Image.GestureRecognizers>
                <Image.Source>
                    <OnPlatform x:TypeArguments="ImageSource">
                        <OnPlatform.iOS>
                            <FileImageSource File="england_flag"/>
                        </OnPlatform.iOS>
                        <OnPlatform.Android>
                            <FileImageSource File="england_flag.png"/>
                        </OnPlatform.Android>
                    </OnPlatform>
                </Image.Source>
        </Image>
    </Grid>
</Frame>
抽头事件:

private void IbtnEnglisch_Clicked(object sender, EventArgs e)
{            
    imgEnglisch.Source = ImageSource.FromResource("Images.england_flag_mercury.jpg");

    //also tried
    imgEnglisch.Source = ImageSource.FromResource("VoTa.Images.england_flag_mercury.jpg", 
    typeof(EmbeddedImage).GetTypeInfo().Assembly);

}
如果我点击图像,则不会显示任何内容。ONBUILD的image属性设置为EMBADDED RESOURCE

如果我用下面的代码调试它,就可以正确地找到该文件

var assembly = typeof(App).GetTypeInfo().Assembly;
foreach (var res in assembly.GetManifestResourceNames())
{
  System.Diagnostics.Debug.WriteLine("found resource: " + res);
}
你能帮我吗


谢谢。

当您使用
嵌入式资源
构建操作将表单项目的图像文件夹(如下面的屏幕截图)放在下面时,您可以使用下面的代码设置加载资源

截图:

代码:

截图:


请将图像添加到Android和iOS资源文件夹中 两个资源文件夹中的名称将相同 在此之后,指定中的唯一名称

图片来源(“英格兰国旗”)


我试过了,但不起作用。如果我点击图片,最初的图片是隐藏的,没有其他图片显示。我已经上传了我的截图。代码运行良好。请检查您的图像生成操作和图像的参数(项目名称、文件夹名称、图像文件名)。正如您在我的问题中看到的屏幕截图所示,图像设置在图像文件夹中,我再次仔细检查了参数,它们是正确的。它可能与XAML有关吗?我在XAML中为每个平台设置IMAGE.SOURCE?我使用了与您相同的XAML。等一会儿。我会上传项目。你可以在GitHub上查看源文件。谢谢,现在可以用了。但有一个问题:为什么不可能在项目中创建一个图像文件夹并使用它?这是可能的,但您必须为此做额外的工作。这是最好的方法,因为您将添加不同大小的图像,如Android drawable hdpi、drawable mdpi文件夹和iOS加载项资源。
var assembly = typeof(App).GetTypeInfo().Assembly;
foreach (var res in assembly.GetManifestResourceNames())
{
  System.Diagnostics.Debug.WriteLine("found resource: " + res);
}
  imgEnglisch.Source= ImageSource.FromResource("XamarinDemo.Images.lighthouse.jpg");

XamarinDemo: Project Name
Images: Folder Name
lighthouse.jpg: Image File Name