我正在尝试在共享项目资源中显示和嵌入图像(如 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/