【发布时间】:2021-04-21 20:38:26
【问题描述】:
我有一个适用于 Android 和 iOS 项目的 Xamarin.Forms 解决方案。
我希望两个应用程序中都包含相同的图像。
我知道我可以将相同的图像分别添加到两个项目代码中,但这听起来不太优雅。
我希望共享项目中的代码中只有一个图像被两个项目引用。
我的图片在共享项目中,并且是Embedded Resource
SharedProject
|
- Resources
|
- icon192.png
我的 XAML(共享项目)
<ContentPage xmlns:myExtensions="clr-namespace:LibraryApp.Extensions;assembly=App">
<Image x:Name="image2"
VerticalOptions="Center" HeightRequest="50" Aspect="AspectFit" />
<Image Source="{myExtensions:ImageResourceExtension Resources.icon192.png}"
VerticalOptions="Center" HeightRequest="50" Aspect="AspectFit" />
</ContentPage>
我的代码隐藏(共享项目)
public partial class AboutPage : ContentPage
{
public AboutPage()
{
InitializeComponent();
image2.Source = ImageSource.FromResource("LibraryApp.AndroidClient.Resources.icon192.png");
}
}
第一张图片,image2完美运行,当从代码隐藏设置时。
但我不想在代码隐藏中处理图像,因为它们是演示细节,而不是实现。
所以我尝试解决这个问题的扩展方法
namespace LibraryApp.Extensions
{
[Preserve(AllMembers = true)]
[ContentProperty(nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null) return null;
// do some work to get the name of the correct assembly/project, here
// var stream = Assembly.GetCallingAssembly().GetManifestResourceStream(Source);
// just for testing, return something we know that works
return ImageSource.FromResource("LibraryApp.AndroidClient.Resources.icon192.png");
}
}
}
此扩展方法确实从另一张图片中调用(使用正确的Source 参数)。
即使代码实际上完全相同相同,我也无法使用扩展方法获取图像。
我收到了FileImageSourceHandler: Could not find image or image file was invalid。
你会如何解决这个问题?
【问题讨论】:
标签: image xamarin.forms code-behind imagesource markup-extensions