Xamarin数据绑定作为嵌入式图像的来源

时间:2019-03-12 14:45:12

标签: c# xaml xamarin xamarin.forms

我正在尝试加载{Binding Icon}数据绑定中定义的图像。 例如:{Binding Icon} = fire.png,因此local:ImageResource将加载嵌入式资源App.Pictures.QuestionIcons.fire.png

的Imageresource
<Image Source="{local:ImageResource App.Pictures.QuestionIcons.{Binding Icon}}"/>

被嘲笑的是将字符串“ App.Pictures.QuestionIcons.fire.png”转换为ImageSource的类

namespace App.Pictures
    {
        [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;

                //Output of this one would be:
                //"DoepkeLeitfaden.Pictures.QuestionIcons.{x:Binding Icon"
                Console.WriteLine(Source);

                // Do your translation lookup here, using whatever method you require
                var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);

                return imageSource;
            }
        }

}

提前谢谢

0 个答案:

没有答案
相关问题