将图像 URL 从 Xamarin Forms PCL 加载到 Android 项目中的 PageRenderer 失败
Loading an image URL from Xamarin Forms PCL to PageRenderer in Android project fails
我正在传递来自网站的图像的 URL 字符串,这些图像出现在 PCL 项目的列表视图中,图像的 URL 传递给Android 自定义渲染器,但使用 MonoDroidToolKit 加载图像失败并出现以下错误:
未处理的异常:
System.ArgumentNullException: 值不能为空。
参数名称:key
来自PCL(传递附件信息):
public partial class ImageViewPage : ContentPage
{
public ImageViewPage(Attachment imageItem)
{
if (imageItem == null)
throw new ArgumentNullException();
BindingContext = imageItem;
_imageItem = imageItem.url;
InitializeComponent();
}
public string _imageItem { get; private set; }
}
在 Android 自定义渲染器中:
[assembly: ExportRenderer(typeof(ImageViewPage),typeof(ImageViewRenderer))]
namespace BibleCodesApp.Droid
{
public class ImageViewRenderer : PageRenderer
{
global::Android.Views.View view;
ImageLoader imageLoader;
Activity activity;
ImageView imageView;
private string imageItem
{
get
{
var imageViewPage = Element as ImageViewPage;
return imageViewPage == null
? null
: imageViewPage._imageItem;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Page>e)
{
base.OnElementChanged(e);
activity = this.Context as Activity;
view = activity.LayoutInflater.Inflate
(Resource.Layout.ImageView,this,false);
imageLoader = new ImageLoader(activity, 512);
imageView = FindViewById<ImageView>(Resource.Id.image_view);
imageLoader.DisplayImage(imageItem, imageView, -1);
}
}
}
Android中的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<BibleCodesApp.Droid.ScaleImageView
android:id="@+id/image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:adjustViewBounds="true" />
</LinearLayout>
</RelativeLayout>
我现在看到了:
您正在夸大您的新视图,但从未真正将其添加到页面!
你应该打电话给
this.ViewGroup.AddView(view);
在 imageLoader.DisplayImage
通话结束后 OnElementChanged
这是正确的解决方案:
[assembly: ExportRenderer(typeof(ZoomImage), typeof(ZoomImageRenderer))]
命名空间WPAppTemplate.Droid
{
public class 缩放图像渲染器:图像渲染器
{
私人 ZoomImage _zoomImage;
私有 ScaleImageView _scaleImage;
protected async override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
_zoomImage = (ZoomImage)e.NewElement;
// create the scale image and set it as the native control so it's available
_scaleImage = new ScaleImageView(Context, null);
_scaleImage.ZoomImage = _zoomImage;
SetNativeControl(_scaleImage);
await LoadImage();
}
}
protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ZoomImage.AspectProperty.PropertyName
|| e.PropertyName == ZoomImage.HeightProperty.PropertyName
|| e.PropertyName == ZoomImage.WidthProperty.PropertyName)
{
_scaleImage.ZoomToAspect();
}
else if (e.PropertyName == ZoomImage.SourceProperty.PropertyName)
{
await LoadImage();
_scaleImage.ZoomToAspect();
}
else if (e.PropertyName == ZoomImage.CurrentZoomProperty.PropertyName)
{
_scaleImage.ZoomFromCurrentZoom();
}
else if (e.PropertyName == ZoomImage.MaxZoomProperty.PropertyName)
{
_scaleImage.UpdateMaxScaleFromZoomImage();
}
else if (e.PropertyName == ZoomImage.MinZoomProperty.PropertyName)
{
_scaleImage.UpdateMinScaleFromZoomImage();
}
}
private async Task LoadImage()
{
var image = await (new ImageLoaderSourceHandler()).LoadImageAsync(_zoomImage.Source, Context);
try
{
if (image != null && image.ByteCount > 0)
_scaleImage.SetImageBitmap(image);
}
catch (Exception e)
{
// catch an image loading failure
Console.WriteLine($"Unable to load bitmap. Exception: {e.Message}");
}
}
}
我正在传递来自网站的图像的 URL 字符串,这些图像出现在 PCL 项目的列表视图中,图像的 URL 传递给Android 自定义渲染器,但使用 MonoDroidToolKit 加载图像失败并出现以下错误:
未处理的异常:
System.ArgumentNullException: 值不能为空。 参数名称:key
来自PCL(传递附件信息):
public partial class ImageViewPage : ContentPage
{
public ImageViewPage(Attachment imageItem)
{
if (imageItem == null)
throw new ArgumentNullException();
BindingContext = imageItem;
_imageItem = imageItem.url;
InitializeComponent();
}
public string _imageItem { get; private set; }
}
在 Android 自定义渲染器中:
[assembly: ExportRenderer(typeof(ImageViewPage),typeof(ImageViewRenderer))]
namespace BibleCodesApp.Droid
{
public class ImageViewRenderer : PageRenderer
{
global::Android.Views.View view;
ImageLoader imageLoader;
Activity activity;
ImageView imageView;
private string imageItem
{
get
{
var imageViewPage = Element as ImageViewPage;
return imageViewPage == null
? null
: imageViewPage._imageItem;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Page>e)
{
base.OnElementChanged(e);
activity = this.Context as Activity;
view = activity.LayoutInflater.Inflate
(Resource.Layout.ImageView,this,false);
imageLoader = new ImageLoader(activity, 512);
imageView = FindViewById<ImageView>(Resource.Id.image_view);
imageLoader.DisplayImage(imageItem, imageView, -1);
}
}
}
Android中的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<BibleCodesApp.Droid.ScaleImageView
android:id="@+id/image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:adjustViewBounds="true" />
</LinearLayout>
</RelativeLayout>
我现在看到了: 您正在夸大您的新视图,但从未真正将其添加到页面!
你应该打电话给
this.ViewGroup.AddView(view);
在 imageLoader.DisplayImage
通话结束后 OnElementChanged
这是正确的解决方案:
[assembly: ExportRenderer(typeof(ZoomImage), typeof(ZoomImageRenderer))]
命名空间WPAppTemplate.Droid { public class 缩放图像渲染器:图像渲染器 { 私人 ZoomImage _zoomImage; 私有 ScaleImageView _scaleImage;
protected async override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
_zoomImage = (ZoomImage)e.NewElement;
// create the scale image and set it as the native control so it's available
_scaleImage = new ScaleImageView(Context, null);
_scaleImage.ZoomImage = _zoomImage;
SetNativeControl(_scaleImage);
await LoadImage();
}
}
protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ZoomImage.AspectProperty.PropertyName
|| e.PropertyName == ZoomImage.HeightProperty.PropertyName
|| e.PropertyName == ZoomImage.WidthProperty.PropertyName)
{
_scaleImage.ZoomToAspect();
}
else if (e.PropertyName == ZoomImage.SourceProperty.PropertyName)
{
await LoadImage();
_scaleImage.ZoomToAspect();
}
else if (e.PropertyName == ZoomImage.CurrentZoomProperty.PropertyName)
{
_scaleImage.ZoomFromCurrentZoom();
}
else if (e.PropertyName == ZoomImage.MaxZoomProperty.PropertyName)
{
_scaleImage.UpdateMaxScaleFromZoomImage();
}
else if (e.PropertyName == ZoomImage.MinZoomProperty.PropertyName)
{
_scaleImage.UpdateMinScaleFromZoomImage();
}
}
private async Task LoadImage()
{
var image = await (new ImageLoaderSourceHandler()).LoadImageAsync(_zoomImage.Source, Context);
try
{
if (image != null && image.ByteCount > 0)
_scaleImage.SetImageBitmap(image);
}
catch (Exception e)
{
// catch an image loading failure
Console.WriteLine($"Unable to load bitmap. Exception: {e.Message}");
}
}
}