简体   繁体   中英

How to pass event from ViewCell to parent object, where ListView is placed

I want to handle the events from the cell within the parent object. This is the XAML of the parent object:

<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.Views.SomeView"
             xmlns:customViews="clr-namespace:App.Views;assembly=App"
             VerticalOptions="FillAndExpand">

  <ListView x:Name="MyList"
            HasUnevenRows="True"
            CachingStrategy="RecycleElement">

    <ListView.ItemTemplate>
      <DataTemplate>
        <customViews:CustomListItem/>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>

As you can see I have a custom cell defined in my data template. The cell itselfs looks something like this:

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="App.Views.CustomListItem">

    <Image x:Name="infoButton" Aspect="AspectFit" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />

</ViewCell>

In the code behind file I add a tap recognizer to the image:

public partial class CustomListItem : ViewCell
{
    private TapGestureRecognizer onIconTapppedRecognizer;

    public CustomListItem()
    {
        InitializeComponent();

        // Make it tapable
        this.onIconTapppedRecognizer = new TapGestureRecognizer();
        this.onIconTapppedRecognizer.Tapped += Icon_Tapped;
        this.infoButton.GestureRecognizers.Add(this.onIconTapppedRecognizer);
    }

    private async void Icon_Tapped(object sender, EventArgs e)
    {
        await MainPage.Instance.Detail.Navigation.PushAsync(new MyNewPage(this.viewModel));
    }
}

Now I want to receive the Icon_Tapped event on SomeView . This approach doesn't work, because the cell is defined in a separate object and not on the same "page". Another approach requires to pass the view model in the code via constructor, but I have defined that in XAML. Furthermore, I have a model for each entry in the list view and not one for the whole list view.

How can I receive the event from the cell?

Didn't know that you can define custom events on any object:

SomeView XAML now looks like this

<customViews:CustomListItem ButtonClicked="SomeView_ButtonClicked"/>

The according event handler in the code behind file from SomeView

private async void SomeView_ButtonClicked(object sender, CustomEventArgs e)
{
    // do something
}

On my ViewCell CustomListItem I have defined a public event handler like this

public event EventHandler<CustomEventArgs> ButtonClicked;

And from the event handler from my added gesture recognizer, this event is invoked:

private void Icon_Tapped(object sender, EventArgs e)
{
    this.ButtonClicked?.Invoke(sender, new CustomEventArgs(this.model));
}

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