Xamarin Forms:来自外部存储的backgroundImage [英] Xamarin Forms: backgroundImage from external storage

查看:394
本文介绍了Xamarin Forms:来自外部存储的backgroundImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Xamarin表单,它将图像成功写入外部存储,然后将其用作ContentPage的背景。

I am developing a Xamarin Forms which writes successfully an image to external storage and then should use it as Background of a ContentPage.

在ContentPage的构造函数中,我写了这个:

In the constructor of the ContentPage I wrote this:

this.BackgroundImage = "/storage/emulated/0/DCIM/D72D01AEF71348CDBFEED9D0B2F259F7.jpg"

但背景图片从不显示。

我检查了Android Manifest并正确设置了读写外部存储的权限。

I checked the Android Manifest and the permissions of read and write external storage are set correctly.

我缺少什么?

推荐答案

您的代码存在的问题是 BackgroundImage 期望与之捆绑的图片你的应用。用于更新背景图片的Android实现如下:

The problem with your code is that BackgroundImage expects an image that's bundled with your app. Android implementation for updating the background image is here:

void UpdateBackgroundImage(Page view)
{
    if (!string.IsNullOrEmpty(view.BackgroundImage))
        this.SetBackground(Context.Resources.GetDrawable(view.BackgroundImage));
}

GetDrawable 方法预期来自应用程序资源的图像,在您的情况下显然不存在。

GetDrawable method expects an image from your application's Resources which obviously doesn't exist in your case.

您应该做的是使用名为ExternalBackgroundImage的新BindableProperty创建自定义渲染器。然后,您可以在Android特定的自定义渲染器中处理外部图像的加载作为背景。

What you should do, is create a custom renderer with a new BindableProperty called ExternalBackgroundImage. Then you could handle loading of the external image as a background in the Android specific custom renderer.

PCL项目

请记住将当前页面从 ContentPage 更改为 ExternalBackgroundImagePage ,以便您可以访问 ExternalBackgroundImage 属性。

Remember to change your current page from ContentPage to ExternalBackgroundImagePage so that you have access to the ExternalBackgroundImage property.

public class ExternalBackgroundImagePage : ContentPage 
{
    public static readonly BindableProperty ExternalBackgroundImageProperty = BindableProperty.Create("ExternalBackgroundImage", typeof(string), typeof(Page), default(string));

    public string ExternalBackgroundImage
    {
        get { return (string)GetValue(ExternalBackgroundImageProperty); }
        set { SetValue(ExternalBackgroundImageProperty, value); }
    }
}

Android项目

[assembly:ExportRenderer (typeof(ExternalBackgroundImagePage), typeof(ExternalBackgroundImagePageRenderer))]
namespace YourProject.Droid
{
    public class ExternalBackgroundImagePageRenderer : PageRenderer 
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            Page view = e.NewElement;
            base.OnElementChanged(e);

            UpdateExternalBackgroundImage(view);
        }

        void UpdateExternalBackgroundImage(Page view)
        {
            if (string.IsNullOrEmpty(view.ExternalBackgroundImage)) 
              return;   

            // Retrieve a bitmap from a file
            var background = BitmapFactory.DecodeFile(view.ExternalBackgroundImage);  

            // Convert to BitmapDrawable for the SetBackground method
            var bitmapDrawable = new BitmapDrawable(background);

            // Set the background image
            this.SetBackground(bitmapDrawable);
        }
    }
}

用法

this.ExternalBackgroundImage = "/storage/emulated/0/DCIM/D72D01AEF71348CDBFEED9D0B2F259F7.jpg"

这篇关于Xamarin Forms:来自外部存储的backgroundImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆