2017-06-01 95 views
0

Visual Studio Project Image Xamarin的新品牌和我浪费了一天多的时间试图让一个简单的图像显示在一个简单的页面上。Xamarin表格图像/嵌入式图像问题

我使用Visual Studio 2017

的图像被设置为嵌入的资源(见所附图片)。运行时没有错误,图像根本不显示。

我曾尝试以下:

<Image Source="XF0003.Assets.logo.png" /> 
<Image Source="Assets.logo.png" /> 
<Image Source="logo.png" /> 
<Image Source="{local:ImageResource XF0003.Assets.logo.png}" /> 
<Image Source="{local:ImageResource Assets.logo.png}" /> 
<Image Source="{local:ImageResource logo.png}" /> 

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" 
      xmlns:local="clr-namespace:XF0003" 
      x:Class="XF0003.MainPage"> 

    <StackLayout> 
     <Image Source="Assets.logo.png" /> 
     <Label Text="Username" /> 
     <Entry x:Name="usernameEntry" Placeholder="username" /> 
     <Label Text="Password" /> 
     <Entry x:Name="passwordEntry" IsPassword="true" /> 
     <Button Text="Login" Clicked="PageOne" /> 
     <Label x:Name="messageLabel" /> 
    </StackLayout> 
</ContentPage> 

XAML.CS页面代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace XF0003 
{ 
    public partial class MainPage : ContentPage 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private async void PageOne(object sender, EventArgs e) 
     { 
      Navigation.InsertPageBefore(new Page1(), this); 
      await Navigation.PopAsync(); 
     } 
    } 
    [ContentProperty("Source")] 
    public class ImageResourceExtension : Xamarin.Forms.Xaml.IMarkupExtension 
    { 
     public string Source { get; set; } 

     public object ProvideValue(IServiceProvider serviceProvider) 
     { 
      if (Source == null) 
      { 
       return null; 
      } 
      // Do your translation lookup here, using whatever method you require 
      var imageSource = ImageSource.FromResource(Source); 

      return imageSource; 
     } 
    } 
} 
+0

您忘记了添加图像 –

+0

不知道发生了什么,但我又将图像添加到我的文章... – jf974

回答

1

在你链接的图像,它看起来像您只将图像保存在xamarin.forms项目中。我相信这些图像需要保存在各自的项目中。

XF0003.Droid/Resources/Drawable (For Android) 

XF0003.iOS/Resources (For iOS) 

确保您的图像具有相同的名称。

对于Android图片使生成操作AndroidResource。 对于iOS图片使生成操作BundleResource。

DoNotCopy for both。

当你指的是图像,只需使用[名]。[文件]

<Image Source="logo.png" /> 

它也非常详细here解释。

+0

我从您提供的链接的嵌入式图像部分拿走了我的大部分代码。从该页的该部分,图像看起来像是项目级别,而不是单个项目级别。但是,我将图像移动到了Android/Resources/Drawable文件夹,将其设置为AndroidResources,并且仍然没有显示图像。我使用了两个:。两人都没有显示图像。 – jf974

+0

我不确定你指的是什么。这里是链接到解释它的确切部分。我也为我的代码使用了相同的页面。 https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Local_Images – mac10688

+1

当你在Xamarin表单中引用图片时,只是指它是logo.png – mac10688