微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin.Forms 在共享项目中设置图像源

如何解决Xamarin.Forms 在共享项目中设置图像源

我有一个包含 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

你会如何解决这个问题?

解决方法

典型的,在花了一天的时间之后,我在问这个问题后 20m 才算出来......

一种解决方案是添加一个 IValueConverter

public class ResourceConverter : IValueConverter
{

    public Object Convert(Object value,Type targetType,Object parameter,System.Globalization.CultureInfo culture)
    {
        if (value is not String filename) throw new ArgumentNullException(nameof(value));

        var nameOfAssembly = Assembly.GetExecutingAssembly().GetName().Name;
        return ImageSource.FromResource($"{nameOfAssembly}.{filename}");
    }

    public Object ConvertBack(Object value,System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

然后在 XAML 中

<ContentPage xmlns:converter="clr-namespace:GPS4Flight.Converter">
    <ContentPage.Resources>
        <converter:ResourceConverter x:Key="ResourceConverter" />
    </ContentPage.Resources>

    <Image Source="{Binding Source=Resources.icon192.png,Converter={StaticResource ResourceConverter}}"
           VerticalOptions="Center" HeightRequest="50" Aspect="AspectFit"  />
</ContentPage>

请注意,我需要 {Binding Source=[nameOfFile] 才能使其工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?