简体   繁体   English

图片不会显示在<img>在 Xamarin Forms

[英]Image won't display in <Image> in Xamarin Forms

I am trying to load a png image into an Image control in Xamarin Forms.我正在尝试将 png 图像加载到 Xamarin Forms 中的图像控件中。 I have added the image to both the drawable and the drawable-hdpi folder, set the BuildAction = AndroidResource and the Custom Tool = MSBuild:UpdateGeneratedFiles.我已将图像添加到 drawable 和 drawable-hdpi 文件夹,设置 BuildAction = AndroidResource 和 Custom Tool = MSBuild:UpdateGeneratedFiles。 One of the png images I try to use, monkeyface.png, copied from a sample project, loads just fine.我尝试使用的其中一个 png 图像,monkeyface.png,从示例项目中复制,加载得很好。 The other png image will not display at all.另一个 png 图像根本不会显示。 Here are the relevant details:以下是相关细节:

CODE代码

            <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                <Image  Source="monkeyface.png" Aspect="AspectFill" HorizontalOptions="CenterAndExpand" VerticalOptions="Start"/>
                <Image  Source="homepage.png" Aspect="AspectFill" HorizontalOptions="Fill" VerticalOptions="End"/>
                <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center">
                    <Button Text="Click" TextColor="Black" HorizontalOptions="FillAndExpand"/>
                </StackLayout>
            </Grid>

OUTPUT OUTPUT

在此处输入图像描述

IMAGE INFO FOR IMAGE 1图像 1 的图像信息

在此处输入图像描述

IMAGE INFO FOR IMAGE 2图像 2 的图像信息

在此处输入图像描述

I am going crazy trying to figure this out.我要疯了,想弄清楚这一点。 Any help would be greatly appreciated!任何帮助将不胜感激!

UPDATE I also tried, with no success, the following code:更新我也尝试了以下代码,但没有成功:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="NGIC_XAML.Views.Login.Login">
    <ContentPage.Content>
        <StackLayout>
            <Image  Source="homepage.png" Aspect="AspectFit"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

But this works:但这有效:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="NGIC_XAML.Views.Login.Login">
    <ContentPage.Content>
        <StackLayout>
            <Image  Source="monkeyface.png" Aspect="AspectFit"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

UPDATE 2更新 2

    <AndroidResource Include="Resources\drawable\homepage.png">
      <Generator>MSBuild:UpdateGeneratedFiles</Generator>
    </AndroidResource>
  <ItemGroup>
    <AndroidResource Include="Resources\drawable-hdpi\homepage.png">
      <Generator>MSBuild:UpdateGeneratedFiles</Generator>
    </AndroidResource>
  </ItemGroup>

And in case someone points out that the image that won't load has the Generator specified, I removed that also and it STILL doesn't load.如果有人指出不会加载的图像指定了生成器,我也将其删除,但它仍然无法加载。

  <ItemGroup>
    <AndroidResource Include="Resources\drawable\monkeyface.png" />
  </ItemGroup>
  <ItemGroup>
    <AndroidResource Include="Resources\drawable-hdpi\monkeyface.png" />
  </ItemGroup>

the png file in question有问题的 png 文件

在此处输入图像描述

The location of the images in the Android Resources folder Android Resources 文件夹中图像的位置

在此处输入图像描述

After removing bin and obj folders and rebuilding, I get the following errors:删除 bin 和 obj 文件夹并重建后,出现以下错误:

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:Object 引用未设置为 object 的实例。 at Xamarin.AndroidDesigner.DesignerProject.set_PreferredTheme(Theme value) in E:\A_work\711\s\Xamarin.Designer.Android\Xamarin.AndroidDesigner\DesignerProject.cs:line 901 at Xamarin.AndroidDesigner.AndroidDesignerSession.SetTheme(Theme theme, Boolean triggerChange) in E:\A_work\711\s\Xamarin.Designer.Android\Xamarin.AndroidDesigner\AndroidDesignerSession.cs:line 376 at Xamarin.Designer.Forms.Preview.AndroidXamlRenderer.GetRenderSessionForFormFactor(XamlRenderContext context, XamlFormFactor formFactor) in E:\A_work\711\s\Xamarin.Designer.Forms\Xamarin.Designer.Forms.Preview\Android\AndroidXamlRenderer.cs:line 236 at Xamarin.Designer.Forms.Preview.AndroidXamlRenderer.d__25.MoveNext() in E:\A_work\711\s\Xamarin.Designer.Forms\Xamarin.Designer.Z6450242531912981C3683CAE88A在 Xamarin.AndroidDesigner.DesignerProject.set_PreferredTheme(主题值)在 E:\A_work\711\s\Xamarin.Designer.Android\Xamarin.AndroidDesigner\DesignerProject.cs:line 901 在 Z1B4419ABC43A358B41D0CAAndroid5077A63908. Boolean triggerChange) in E:\A_work\711\s\Xamarin.Designer.Android\Xamarin.AndroidDesigner\AndroidDesignerSession.cs:line 376 at Xamarin.Designer.Forms.Preview.AndroidXamlRenderer.GetRenderSessionForFormFactor(XamlRenderContext context, XamlFormFactor formFactor) in E :\A_work\711\s\Xamarin.Designer.Forms\Xamarin.Designer.Forms.Preview\Android\AndroidXamlRenderer.cs:line 236 at Xamarin.Designer.Forms.Preview.AndroidXamlRenderer.d__25.MoveNext() in E:\ A_work\711\s\Xamarin.Designer.Forms\Xamarin.Designer.Z6450242531912981C3683CAE88A 32A66Z.Preview\Android\AndroidXamlRenderer.cs:line 136 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Xamarin.Designer.Forms.Preview.AndroidXamlRenderer.d__27.MoveNext() in E:\A_work\711\s\Xamarin.Designer.Forms\Xamarin.Designer.Forms.Preview\Android\AndroidXamlRenderer.cs:line 307 --- End of stack trace from previous location where exception was thrown --- at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject) 32A66Z.Preview\Android\AndroidXamlRenderer.cs:第 136 行 --- 在 System.Runtime.CompilerServices 的 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处从先前引发异常的位置结束堆栈跟踪。 TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Xamarin.Designer.Forms.Preview.AndroidXamlRenderer.d__27.MoveNext() in E:\A_work\711\s\Xamarin.Designer .Forms\Xamarin.Designer.Forms.Preview\Android\AndroidXamlRenderer.cs:line 307 --- 堆栈跟踪从先前抛出异常的位置结束---在 Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception异常对象)

You need to set Grid number of rows and columns, then specify in each child which row and column it should be on.您需要设置网格的行数和列数,然后在每个子项中指定它应该位于哪一行和哪一列。 As this example has only one column, it does not need to be specified as good code practice in XAML.由于此示例只有一列,因此不需要在 XAML 中将其指定为良好的代码实践。

<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

    <Grid.RowDefinitions>
        <RowDefinition Height="49*" />
        <RowDefinition Height="49*" />
        <RowDefinition Height="2*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Image Source="monkeyface.png" Aspect="AspectFill" HorizontalOptions="CenterAndExpand" />
    <Image Grid.Row="1" Source="homepage.png" Aspect="AspectFill" HorizontalOptions="Fill" />
    <Button Grid.Row="2" Text="Click" TextColor="Black" HorizontalOptions="FillAndExpand"/>

</Grid>

So, after 2 days of frustration in trying to get the png file to display in an Image, and after following all of the great suggestions posted in response to my question, I decided to perform the Visual Studio update from the version I was using, Version 16.6.0, to Version 16.6.1.因此,在尝试让 png 文件显示在图像中而感到沮丧 2 天之后,在遵循了针对我的问题发布的所有重要建议之后,我决定从我正在使用的版本执行 Visual Studio 更新,从版本 16.6.0 到版本 16.6.1。 After the update finished, I loaded the solution again and LO AND BEHOLD.更新完成后,我再次加载了解决方案,然后你看。 the image displays perfectly, I still have no definitive reason why it was failing to load before I did the update.图像显示完美,我仍然没有明确的原因为什么在我更新之前它无法加载。 but WHO CARES!但谁在乎! It loads now and that's all that matters.它现在加载,这就是最重要的。

Thanks to everyone who tried to help!感谢所有试图提供帮助的人!

The issue is not with your image, but with your incorrect use of a layout for what you are trying to achieve.问题不在于您的图像,而在于您错误地使用了您想要实现的布局。 If you want the items to be one below the other, then the best layout will be a StackLayout .如果您希望项目一个在另一个之下,那么最好的布局将是StackLayout

Currently, you are using a Grid layout and you have added all of its items without even specifying the row definitions.目前,您正在使用Grid布局,并且您已经添加了它的所有项目,甚至没有指定行定义。 This way you are just trying to "align" them by their HorizontalOptions & VerticalOptions , which is not the best approach, since the grid is trying to guess what you want your end result to look like.这样你只是试图通过他们的HorizontalOptionsVerticalOptions “对齐”它们,这不是最好的方法,因为网格试图猜测你想要你的最终结果是什么样的。

If you still want to use a Grid for your layout, then I strongly suggest you read thoroughly grid's Rows & columns guide.如果您仍想在布局中使用Grid ,那么我强烈建议您通读 grid 的Rows & columns指南。

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

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