简体   繁体   中英

Custom picker renderer draw event not recognized in Xamarin.Forms for WinPhone

I am trying to customize Xamarin.Forms picker control. In android and ios I am overriding the Draw event and drawing the control myself. However on the WinPhone 8.1 project I am unable to override the Draw event because it is not recognized. Below is the code I am using in android and ios and a screen capture of the WinPhone project showing that the draw event doesnt exist.

Android

using Android.Graphics;
using Namespace.CustomControls;
using Namespace.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRendererAttribute(typeof(BindablePicker), typeof(BindablePickerRenderer))]
namespace Namespace.Droid.Renderers
{
    public class BindablePickerRenderer : PickerRenderer
    {
        public BindablePickerRenderer()
        {
            this.SetWillNotDraw(false);
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            this.Element.PropertyChanged += (s_, e_) => Invalidate();
        }

        public override void Draw(Canvas canvas)
        {
            BindablePicker bp = (BindablePicker)this.Element;

            // Code to draw my control
        }
    }
}

IOS

using System;
using System.Collections.Generic;
using System.Text;
using CoreGraphics;
using Xamarin.Forms.Platform.iOS;
using Namespace.CustomControls;
using UIKit;
using Xamarin.Forms;
using Namespace.iOS.Renderers;

[assembly: ExportRendererAttribute(typeof(BindablePicker), typeof(BindablePickerRenderer))]
namespace Namespace.iOS.Renderers
{
    public class BindablePickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            this.Element.PropertyChanged += (s_, e_) => SetNeedsDisplay();
        }
        public override void Draw(CGRect rect)
        {
            BindablePicker bp = (BindablePicker)this.Element;

            // Code to draw my control
        }
    }
}

WinPhone

唯一可用的事件是OnApplyTemplate

If anyone has any idea on how I can get into the draw event of this control on the Windows Phone 8.1 platform I'd greatly appreciate it.

Thanks in advance.

WindowsPhone does not have direct drawing abilities.

You will want to create a control hierarchy of some sort to get around your needs for the Windows Phone platform.

If you want to do custom drawing, use something like a WriteableBitmap and render into an image control for example.

Alternatively maybe look at NGraphics , with a wrapper control for it for Xamarin.Forms here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM