【问题标题】:Xamarin Forms Image/Embedded Image ProblemsXamarin 形成图像/嵌入式图像问题
【发布时间】:2017-06-03 03:40:47
【问题描述】:

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;
        }
    }
}

【问题讨论】:

  • 你忘记添加图片了
  • 不知道发生了什么,但我再次将图片添加到我的帖子中......

标签: xamarin.android xamarin.forms


【解决方案1】:

在您链接的图像中,您似乎只将图像保存在 xamarin.forms 项目中。我认为图像需要保存在各自的项目中。

 XF0003.Droid/Resources/Drawable (For Android)

 XF0003.iOS/Resources (For iOS)

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

为Android图片制作构建动作AndroidResource。 为iOS图片制作构建动作BundleResource。

两者都不要复制。

当您引用图像时,只需使用 [name].[filetype]

<Image Source="logo.png" />

here也解释的很详细。

【讨论】:

  • 我的大部分代码来自您提供的链接的嵌入式图像部分。从页面的该部分,图像似乎是项目级别,而不是单个项目级别。但是,我将图像移动到 Android/Resources/Drawable 文件夹,将其设置为 AndroidResources,但仍然无法显示要显示的图像。我同时使用了:。两者都没有显示图像。
  • 我不确定你指的是什么。这是解释它的确切部分的链接。我的代码也使用了相同的页面。 developer.xamarin.com/guides/xamarin-forms/user-interface/…
  • 当您在 xamarin 表单中引用图像时,只需引用它是 logo.png
  • 我不知道所有本地:ImageResource。我没有用那个。在您上面的代码中,我不知道 ImageResourceExtension 的用途。我不认为这种方法是必要的。
  • 是的,谢谢,只需使用 Source="logo.png",将图像移动到项目特定文件夹后,就可以了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多