Xamarin Forms 使用 XAML 显示来自嵌入式资源的图像

标签 xamarin xamarin.forms

我正在尝试在共享项目资源中显示和嵌入图像(如 Microsoft 文档中的 here 所述)。

我创建了 ImageResourceExtension,项目编译,但是当我启动项目时出现以下错误:

Could not load type 'Xamarin.Forms.Xaml.IReferenceProvider' from assembly 'Xamarin.Forms.Core, Version=2.0.0.0



这是我的代码:

主页.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:App1"
             x:Class="App1.MainPage">

    <StackLayout>

        <Image x:Name="imgTest" Source="{local:ImageResource img.jpg}" />

    </StackLayout>

</ContentPage>

嵌入式图像文件
namespace App1
{
    [ContentProperty(nameof(Source))]
    public class ImageResourceExtension : 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, typeof(ImageResourceExtension).GetTypeInfo().Assembly);

            return imageSource;
        }
    }
}

最佳答案

你可以试试这个,它对我有用

xyz.xaml.cs

imgName.Source = ImageSource.FromResource("ProjectName.Images.backicon.png");

xyz.xaml
<Image x:Name="imgName" />

希望这段代码对你有帮助!!

关于Xamarin Forms 使用 XAML 显示来自嵌入式资源的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52793957/

相关文章:

c# - ListView xamarin.forms 中所选项目的背景颜色

c# - Xamarin Forms 相对布局 - 获取内部居中的堆栈布局

android - 错误的 Xamarin 形成图像维度

android-intent - Xamarin Android 处理 actionsend mimetype 图像的隐式意图

ios - UIAlertView 弹出窗口不允许我向上滑动控制中心

ios - 工具栏项目未以 xamarin 形式显示

iOS 绑定(bind) : How to create a binding for a class that implements several delegates?

inheritance - Xamarin.Forms:使用所有页面扩展 CommonToolbarPage 类

c# - 是否可以添加对 Xamarin 项目的 System.windows.form 引用?

sqlite - Xamarin.Forms SQLite,在创建表时初始化数据