2017-06-06 59 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 –