我的任务是尝试为我的Xamarin.Forms项目实现可收缩的图像视图组件,这是一个自定义渲染器,但是我无法完成最后的几个步骤。我正在尝试将https://github.com/xamarin/XamarinComponents/tree/master/Android/PhotoView用于本机Android实现。
这是我的自定义视图,位于我的Xamarin.Forms项目中
public class NativeImageViewer : View
{
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
propertyName: "Image",
returnType: typeof(string),
declaringType: typeof(string));
public string Image
{
get { return (string)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
}
然后,这是我在Xamarin.Android项目中的自定义渲染器。
[assembly: ExportRenderer(typeof(NativeImageViewer), typeof(NativeAndroidImageRenderer))]
namespace MapManPrism.Droid.Renderers
{
public class NativeAndroidImageRenderer : ViewRenderer<NativeImageViewer, NativeAndroidImageRenderer>
{
public NativeAndroidImageRenderer(Context context) : base(context)
{
}
PhotoViewAttacher photoViewAttacher;
private readonly Context _context;
private NativeAndroidImageRenderer nativeImageViewer;
protected override void OnElementChanged(ElementChangedEventArgs<NativeImageViewer> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
// somehow here I want to attach photoViewAttacher to an image control
// and then use SetNativeControl? But I can't work it out.
}
}
}
}
}
无论我做什么,我都无法正常工作。有什么想法吗?