【问题标题】:Having issues displaying images Xamarin Forms using only C#仅使用 C# 显示图像 Xamarin 表单时出现问题
【发布时间】:2017-06-06 08:15:37
【问题描述】:

我参考这个网站https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Local_Images问这个问题

我在网站上尝试了 xaml 代码并且它可以工作,但是在我创建了一个新的 xamarin PCL 项目并测试了 c# 代码之后。无法显示图片。

我做了什么: 从 AboutPage.xaml 中删除所有内容,直到它看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EmbbedImages.Views.AboutPage"
             xmlns:vm="clr-namespace:EmbbedImages.ViewModels;"
             Title="{Binding Title}">
  <ContentPage.BindingContext>
    <vm:AboutViewModel />
  </ContentPage.BindingContext>

</ContentPage>

然后对于 AboutPage.xaml.cs,我对其进行了修改,使其如下所示:

using Xamarin.Forms;

namespace EmbbedImages.Views
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();

            var beachImage = new Image { Aspect = Aspect.AspectFit };
            beachImage.Source = ImageSource.FromFile("butterfly.jfif");

        }
    }
}

我已确保将 butterfly.jfif 图像添加到我的 android 可绘制文件夹中,但是没有显示任何图像。由于我是 xamarin 和 Android 以及 c# 的新手,我们将不胜感激。

【问题讨论】:

  • 如果您将图像的扩展名更改为.jpg 是否有效?查看this 资源,似乎不支持.jfif 图像格式。
  • 您的 .jfif 文件实际上是 jpeg 编码文件吗?仅供参考:文件扩展名无关紧要,您可以将 jpeg 扩展名更改为.fubar,Android 将加载并显示它,因为解码器使用文件的内容,而不是扩展名来确定它是什么类型的图像,但它必须是受支持的格式。

标签: c# android xamarin


【解决方案1】:

图片可能不会显示,因为它不是页面的一部分。 如果您已将其添加到页面中,但仍然无法显示,则在加载/打开文件/解码时一定有问题。

您可以将其添加到 XAML 中,也可以将您的代码编辑为以下内容:

using Xamarin.Forms;

namespace EmbbedImages.Views
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();

            var beachImage = new Image { Aspect = Aspect.AspectFit };
            beachImage.Source = ImageSource.FromFile("butterfly.jfif");
            this.Content = beachImage;    
        }
    }
}

在 XAML 中添加它看起来像这样:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EmbbedImages.Views.AboutPage"
             xmlns:vm="clr-namespace:EmbbedImages.ViewModels;"
             Title="{Binding Title}">
  <ContentPage.BindingContext>
    <vm:AboutViewModel />
  </ContentPage.BindingContext>

  <ContentPage.Content>
    <Image x:Name="beachImage" Aspect="AspectFit">
  </ContentPage.Content>

</ContentPage>

以及背后的代码:

using Xamarin.Forms;

namespace EmbbedImages.Views
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();

            beachImage.Source = ImageSource.FromFile("butterfly.jfif");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 2018-07-11
    • 2018-02-13
    • 2022-12-16
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多