【问题标题】:Xamarin.Forms and accessing native control within custom renderersXamarin.Forms 和访问自定义渲染器中的本机控件
【发布时间】:2014-08-13 22:10:15
【问题描述】:

如何获取对特定原生视图的引用,而不是 UIView? 这是我的自定义标签渲染器:

   public class MyLabelRenderer : LabelRenderer
   {
       protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
       {
            //how to get UILabel reference created by base LabelRender?
            base.OnElementChanged(e);
       }
   }

我以Renderer Sample 为例。

【问题讨论】:

    标签: xamarin renderer xamarin.forms


    【解决方案1】:

    尝试使用e.OldElemente.NewElement

    【讨论】:

    • 是 UIView,不是 UILabel。它是运行时 this 所以这是类型
    【解决方案2】:

    您使用的引用已过时。用这个http://forums.xamarin.com/discussion/comment/59303

    可以通过Control 属性访问本机视图,前提是您调用了SetNativeControl()

        protected override void OnElementChanged (ElementChangedEventArgs<MyCustomView> e)
        {
            base.OnElementChanged (e);
    
            if (e.OldElement == null) {
                // perform initial setup
                SetNativeControl (new UIMyCustomView (RectangleF.Empty));
            }
    
            UpdateSomething ();
        }
    

    【讨论】:

    • 确保通过 nuget(或 Xamarin Studio 上的包管理器)更新包。
    【解决方案3】:

    您需要使用渲染器的 Control 属性。它理论上应该在 OnElementChanged 方法中可用。但是,在 UWP 等某些平台上,自定义渲染器实际上就是本机控件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2021-04-28
      • 1970-01-01
      • 2017-01-07
      • 2016-05-31
      相关资源
      最近更新 更多