0

In my MainPage.xaml, I have a CollectionView that uses a ViewModel to populate:

               <CollectionView
                x:Name="ResponseCollectionView"
                ItemsSource="{Binding Responses}"
                BackgroundColor="#404040"
                HeightRequest="200" 
                >

                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="{x:Type x:String}" >
                        <Label Text="{Binding .}" BackgroundColor="#303030" />
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

The MainPage.xaml.cs looks like this:

public partial class MainPage : ContentPage
{
    PingResponseViewModel prvm = new PingResponseViewModel();

    public MainPage(PingResponseViewModel prvm)
    {
        InitializeComponent();
        BindingContext = prvm;

        Debug.WriteLine("---------------------------------");
        Debug.WriteLine("Program started");
    }

}

And the viewmodel is this:

namespace PingTest.ViewModel;

public partial class PingResponseViewModel : ObservableObject
{
    public PingResponseViewModel() 
    {
        Responses = new ObservableCollection<string>();
    }

    [ObservableProperty]
    ObservableCollection<string> responses;
}

But I can't seem to do this in an event handler because of cross threading. I also can't access the CollectionView in the viewmodel when Responses has a string added. I'm currently lost.

mirkaim
  • 145
  • 1
  • 10
  • Probably works just like Xamarin's CollectionView https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/scrolling – kenny May 16 '23 at 02:51
  • yep https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/collectionview/scrolling – kenny May 16 '23 at 02:52

0 Answers0