I have a List that is bound to a property. There is a load event too "ListLoaded".
<ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" BorderThickness="0" HorizontalAlignment="Left" ItemsSource="{Binding DoctorDetailsBooking,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding ListLoaded}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
On its load event, I am setting the 1st selected item to true and changing the background color of that item.
Some operations and API calls are made in the constructor of the ViewModel. The load event is also set in the constructor. It takes some time to load the screen. So I added the entire code in constructor in the Task Factory and set the progress bar visibility accordingly.
Task tskOpen = Task.Factory.StartNew(() =>
{
ProgressBarVisibility = Visibility.Visible;
DataAccess data = new DataAccess();
DoctorDetailsBooking = data.GetDoctorsList(Convert.ToDateTime(BookingDate).Date);
FillSlots();
**ListLoaded = new RelayCommand<RoutedEventArgs>(ListViewLoaded);**
}).ContinueWith(t =>
{
ProgressBarVisibility = Visibility.Hidden;
}, TaskScheduler.FromCurrentSynchronizationContext());
The issue is, when I give the code inside the Task, the ListeViewLoaded event does not fire. Hence, the list view is not loaded properly. If I remove the Task portion of code, the event is fired and everything works well.
I do not know threading and Task concept well. Am I missing something here?