How to Refresh CollectionView from another Content View (Template) Xamarin Forms

Vuyiswa Maseko 351 Reputation points
2021-03-25T22:04:47.237+00:00

Good Day All

I have CollectionView view defined like this

<CollectionView x:Name="LstShares" >
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<template:SharesViewTemplate />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

In the Template "template:SharesViewTemplate" i want to refresh the LstShares , by calling the same function that binded it .

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-26T06:47:47.403+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    A simple method is to use Xamarin.Forms MessagingCenter to achieve this.

    In your page, Subscribe message as follows(PersonCell is my ViewCell):

    	public HomePage ()  
    	{  
    		InitializeComponent ();  
    
    		MessagingCenter.Subscribe<PersonCell,object>(this, "AddItem", (obj, item) =>  
    		{  
    			//var newItem = item as VeggieViewModel;  
    
    
    		});  
    	}  
    

    In ViewCell, send message:

      public partial class PersonCell : ViewCell  
    {  
        public PersonCell()  
        {  
            InitializeComponent();  
    
            object obj = new object();  
    
            MessagingCenter.Send(this, "AddItem", obj);  
        }  
    }  
    

    For more details, check: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments