2017-06-06 39 views
0

我问这个问题,参照本网站https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Local_Images有问题,显示仅使用C#

我想在网站上XAML代码但是它的工作原理,我创建了一个新的xamarin PCL项目和测试后的图像Xamarin形式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#的任何帮助将不胜感激。

+0

如果您将图像的扩展名更改为'.jpg',这是否正常工作?查看[this](https://developer.android.com/guide/topics/media/media-formats.html)资源,它不会显示支持'.jfif'图像格式。 –

+0

你的'.jfif'文件实际上是一个jpeg编码文件吗?仅供参考:文件扩展名无关紧要,您可以将jpeg扩展名更改为'.fubar',Android将加载并显示它,因为解码器使用文件的内容,而不是扩展名来确定它是什么类型的图像,但它必须是支持的格式。 – SushiHangover

回答

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"); 
     } 
    } 
} 
+1

非常感谢它真的有用!酷:D –