首页 文章

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

提问于
浏览
0

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 回答

  • 1

    在您链接的图像中,看起来您只在xamarin.forms项目中保存了图像 . 我相信图像需要保存在各自的项目中 .

    XF0003.Droid/Resources/Drawable (For Android)
    
     XF0003.iOS/Resources (For iOS)
    

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

    对于Android图片,制作动作AndroidResource . 对于iOS图片,使构建动作BundleResource .

    两者都是DoNotCopy .

    当您参考图像时,只需使用[名称] . [filetype]

    <Image Source="logo.png" />
    

    它还详细解释了here .

相关问题