Xamarin.Forms 引用 放在 PCL 中的图片

今天状态及其不好,见谅,Xamarin 技术交流 (偏向 xamarin.android): 1092417123

PCL 中图片资源格式

官方解释 : Embedded Images

Embedded images are also shipped with an application (like local images) but instead of having a copy of the image in each application’s file structure the image file is embedded in the assembly as a resource. This method of distributing images is particularly suited to creating components, as the image is bundled with the code.

To embed an image in a project, right-click to add new items and select the image/s you wish to add. By default the image will have Build Action: None; this needs to be set to Build Action: EmbeddedResource.

Image 设置

beach.jpg 是图片名称

<ContentPage.Content>
    <StackLayout>
        <Label Text = " Embedded Images " />
        <Image Source = "{local:ImageResource WorkingWithImages.beach.jpg}" />
    </StackLayout>
</ContentPage.Content>

代码中 :

var embeddedImage = new Image { Source = ImageSource.FromResource("WorkingWithImages.beach.jpg") };

Using XAML

Because there is no built-in type converter from string to ResourceImageSource, these types of images cannot be natively loaded by XAML. Instead, a simple custom XAML markup extension can be written to load images using a Resource ID specified in XAML :

// You exclude the 'Extension' suffix when using in Xaml markup
    [Preserve(AllMembers = true)]
    [ContentProperty ("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
            string s = Application.Current.Resources["logoEvo"] as string;

            var assembly = typeof(ImageResourceExtension).GetTypeInfo().Assembly;
            var assemblyString = assembly.GetName().Name.ToString();
            var imageSource = ImageSource.FromResource(assemblyString + "." + s);
            return imageSource;
        }
    }

在 App.xaml 中定义 string,以便之后引用

<Application.Resources>
        <ResourceDictionary>
            <x:String x:Key="logoEvo">beach.jpg</x:String>
        </ResourceDictionary>
     </Application.Resources>

Page 中使用

<Image Source="{local:ImageResourceExtension logoEvo}" />

Troubleshooting Embedded Images

Because it is sometimes difficult to understand why a particular image resource isn’t being loaded, the following debug code can be added temporarily to an application to help confirm the resources are correctly configured. It will output all known resources embedded in the given assembly to the Console to help debug resource loading issues.

using System.Reflection;
// ...
// NOTE: use for debugging, not in released app code!
var assembly = typeof(EmbeddedImages).GetTypeInfo().Assembly;
foreach (var res in assembly.GetManifestResourceNames())
{
    System.Diagnostics.Debug.WriteLine("found resource: " + res);
}

88, 调整心态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值