【问题标题】:Custom picker renderer draw event not recognized in Xamarin.Forms for WinPhoneXamarin.Forms for WinPhone 中无法识别自定义选取器渲染器绘制事件
【发布时间】:2016-02-02 14:20:04
【问题描述】:

我正在尝试自定义 Xamarin.Forms 选择器控件。在 android 和 ios 中,我将覆盖 Draw 事件并自己绘制控件。但是在 WinPhone 8.1 项目中,我无法覆盖 Draw 事件,因为它无法识别。下面是我在 android 和 ios 中使用的代码和 WinPhone 项目的屏幕截图,显示绘图事件不存在。

安卓

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

如果有人知道如何在 Windows Phone 8.1 平台上参与此控件的绘制事件,我将不胜感激。

提前致谢。

【问题讨论】:

    标签: c# mobile xamarin windows-phone-8.1 xamarin.forms


    【解决方案1】:

    WindowsPhone 没有直接绘图能力。

    您将需要创建某种控制层次结构来满足您对Windows Phone 平台的需求。

    如果您想进行自定义绘图,请使用 WriteableBitmap 之类的东西并渲染到图像控件中。

    也可以查看NGraphics,为Xamarin.Forms here 提供一个包装控件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2017-01-07
      • 1970-01-01
      相关资源
      最近更新 更多