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.