Forum Xamarin.Forms

How to get current ViewCell in ListView in OnItemAppearing

I am trying to fade in the cells for a ListView, but in order to do that I need to access the current cell being shown in OnItemAppearing. Anyone know how to do this?

Posts

  • Anyone?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Any time you ask yourself "how do I get the view from this view model" you need to stop and back up. You're approaching the problem the wrong way. If you need to communicate with your view from your view model then there are two ways to do that: modifying a property (along with INotifyPropertyChanged) and signaling an event. Pick the one that makes sense and have your cell use that mechanism to know when it needs to do its animation.

  • I'm not using a view model. I'm using the code behind for a XAML page. I also don't understand how, as you're suggesting, I use the INotifyPropertyChanged of a binding to get it's view.

  • @adamkemp Ah. That makes a lot more sense, thanks. Do you happen to have or know of an example on how to subscribe to an event through BindingContext. I'm not quite sure I get how that works in context to the view itself.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Here's an example:

    public class ItemViewModel
    {
        public event EventHandler Appearing;
    }
    
    public class CustomCell : ViewCell
    {
        private Label _label;
        private ItemViewModel _viewModel;
    
        public CustomCell()
        {
            _label = new Label();
            View = _label;
        }
    
        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();
    
            if (_viewModel != null)
            {
                _viewModel.Appearing -= OnItemAppearing;
            }
    
            _viewModel = BindingContext as ItemViewModel;
    
            if (_viewModel != null)
            {
                _viewModel.Appearing += OnItemAppearing;
            }
        }
    
        private void OnItemAppearing(object sender, EventArgs e)
        {
            // Do animation
        }
    }
    

    Along the way I rediscovered the fact that views have an OnAppearing method as well, and you may be able to simplify this approach to avoid having to involve the view model at all. Rather than using the ListView's ItemAppearing event you may be able to just do this:

    public class CustomCell : ViewCell
    {
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            // Do animation
        }
    }
    
  • @adamkemp Perfect!! That last part was exactly what I needed. I was already using a custom view cell so I could change the cell hilight colors, so I just added OnAppearing and OnDisappearing to that and now it works great. Thanks!!! :)

  • amirvenusamirvenus USMember ✭✭✭

    @adamkemp said:
    Here's an example:

    public class ItemViewModel
    {
        public event EventHandler Appearing;
    }
    
    public class CustomCell : ViewCell
    {
        private Label _label;
        private ItemViewModel _viewModel;
    
        public CustomCell()
        {
            _label = new Label();
            View = _label;
        }
    
        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();
    
            if (_viewModel != null)
            {
                _viewModel.Appearing -= OnItemAppearing;
            }
    
            _viewModel = BindingContext as ItemViewModel;
    
            if (_viewModel != null)
            {
                _viewModel.Appearing += OnItemAppearing;
            }
        }
    
        private void OnItemAppearing(object sender, EventArgs e)
        {
            // Do animation
        }
    }
    

    Along the way I rediscovered the fact that views have an OnAppearing method as well, and you may be able to simplify this approach to avoid having to involve the view model at all. Rather than using the ListView's ItemAppearing event you may be able to just do this:

    public class CustomCell : ViewCell
    {
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            // Do animation
        }
    }
    

    Thanks for the code.

    Could you please share an example on using TranslateTo, Scale or Fade effect for the ViewCell?

  • Isha_DawarIsha_Dawar INMember ✭✭

    @adamkemp can we access Viewcell view from with in ItemAppearing Method of listview?

Sign In or Register to comment.