Custom renderer not appearing in listview datatemplate...anyone know why?

DavePerkinsDavePerkins USUniversity ✭✭

I wrote a custom renderer to handle gestures (long press in particular). Debugging the code, the custom renderer is not hitting the breakpoints when used inside a datatemplate/viewcell (as shown in the following snippet)...the Label with a background color of Orange does appear, but the local.LabelExt does not appear (not the text or the background color). The background colors are only there to assist in my troubleshoot efforts.

Xaml:
<?xml version="1.0" encoding="utf-8" ?>

<ContentPage.ToolbarItems>

</ContentPage.ToolbarItems>

<ContentPage.Content>


<ListView.ItemTemplate>


<ViewCell.ContextActions>


</ViewCell.ContextActions>

                        </Label>
                        <local.LabelExt x:Name="lblAuditSetName" 
                                    Text="{Binding Name}" 
                                    Style="{StaticResource BaseLabelStyle}"
                                    HorizontalOptions="StartAndExpand" 
                                    VerticalOptions="Start" 
                                    VerticalTextAlignment="Start" 
                                    HorizontalTextAlignment="Start" 
                                    FontAttributes="Bold"                                           
                                    FontSize="14"
                                    TextColor="Black"
                                    Margin="5,0"
                                    LongPress="OnLongPress"
                                    CommandParameter="{Binding .}"
                                    BackgroundColor="Red">
                        </local.LabelExt>

Extended class:
using System;
using Xamarin.Forms;

namespace NotWorkingNS
{
public class LabelExt : Label
{

region Events

public delegate void LongPressDelegate(object sender, EventArgs e);
public event LongPressDelegate LongPress;

endregion Events

#region Properties
public const string CommandParameterPropertyName = "CommandParameter";
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create
    (CommandParameterPropertyName, typeof(object), typeof(LabelExt), new object());
public object CommandParameter
{
    get => (object)GetValue(CommandParameterProperty);
    set => SetValue(CommandParameterProperty, value);
}

public const string TagPropertyName = "Tag";
public static readonly BindableProperty TagProperty = BindableProperty.Create
    (
        propertyName: TagPropertyName,
        returnType: typeof(object),
        declaringType: typeof(LabelExt),
        defaultValue: new object()
    );
public object Tag
{
    get => (object)GetValue(TagProperty);
    set => SetValue(TagProperty, value);
}
#endregion Properties

public EventHandler LongPressActivated;

public void HandleLongPress(object sender, EventArgs e)
{
    //handle long press event
    LongPress?.Invoke(sender, new LabelExtEventArgs(e, CommandParameter));
}

}

public class LabelExtEventArgs : EventArgs
{
public object CommandParameter { get; set; }
public EventArgs EventArgs { get; set; }

public LabelExtEventArgs(EventArgs e, object CommandParameter)
{
    EventArgs = e;
    this.CommandParameter = CommandParameter;
}

}

}

Custom Renderer:
using System;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using NotWorkingNS;

[assembly: ExportRenderer(typeof(LabelExt), typeof(NotWorkingNS.iOS.LabelExtRenderer))]

namespace NotWorkingNS.iOS
{
public class LabelExtRenderer : LabelRenderer
{
LabelExt view;
public LabelExtRenderer()
{
this.AddGestureRecognizer(new UILongPressGestureRecognizer((longPress) => {
if (longPress.State == UIGestureRecognizerState.Began)
{
view.HandleLongPress(this, new EventArgs());
}
}));
}

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

    if (e.NewElement != null)
        view = e.NewElement as LabelExt;
}

}

}

Best Answer

  • DavePerkinsDavePerkins US ✭✭
    Accepted Answer

    Found the issue...local.LabelExt should be local:LabelExt...I would like to thank the compiler for NOT flagging this and no exceptions being thrown anywhere!

Answers

  • DavePerkinsDavePerkins USUniversity ✭✭
    Accepted Answer

    Found the issue...local.LabelExt should be local:LabelExt...I would like to thank the compiler for NOT flagging this and no exceptions being thrown anywhere!

  • IshaPIshaP Member ✭✭

    Hi, Do you have the solution for android? If there, can you please post it!

Sign In or Register to comment.