创建UIView渲染器

时间:2015-06-26 18:52:13

标签: c# xamarin xamarin.ios xamarin.forms

我在Xamarin中创建渲染器时遇到问题。

我正在尝试在渲染器中显示另一个视图,但它只是不起作用。我该如何设置?我对如何设置这种类型的渲染器感到困惑。是protected override void OnElementChanged (ElementChangedEventArgs<PdfView> e)甚至是必要的?

我有一个像这样的页面类:

public class PdfView : ContentView
    {

    }

    public class PdfPage : ContentPage
    {
        public PdfPage (ProjectViewModel _viewModel)
        {

            this.Content = new PdfView ();
        }
    }

渲染器:

[assembly:ExportRenderer(typeof(PdfView), typeof(PdfViewRenderer))]
namespace iOS
{
    public class PdfViewRenderer : ViewRenderer<PdfView, UIScrollView >
    {

        protected override void OnElementChanged (ElementChangedEventArgs<PdfView> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                return;
            }

            Add (new UIView (){ BackgroundColor = UIColor.Gray});

        }


    }
}

1 个答案:

答案 0 :(得分:2)

而不是打电话

Add (new UIView (){ BackgroundColor = UIColor.Gray});

尝试改为调用SetNativeControl

SetNativeControl(new UIView (){ BackgroundColor = UIColor.Gray});

这应该将您刚刚创建的本机iOS控件绑定到自定义的xamarin.forms视图。