Xamarin形成图像/嵌入式图像问题

时间:2017-06-01 23:54:49

标签: xamarin.android xamarin.forms

Visual Studio Project Image 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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在您链接的图片中,您似乎只在xamarin.forms项目中保存了图像。我相信图像需要保存在各自的项目中。

即。

 XF0003.Droid/Resources/Drawable (For Android)

 XF0003.iOS/Resources (For iOS)

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

为Android图片制作AndroidResource构建动作。 对于iOS图片,请构建动作BundleResource。

两者的DoNotCopy。

当您参考图像时,只需使用[名称]。[文件类型]

<Image Source="logo.png" />

它还详细解释了here

相关问题