The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[WP8.1][C#] Need help with Observable collection collection changed event? RRS feed

  • Question

  • For the past 2 days I am trying to make the new/editted listview item in focus when the user navigates back to the MainPage. On the MainPage clicking on a listview item, takes the user to the next page, where the item can be editted or a new item can be added. The editting & adding is working as expected, as I checked it by adding breakpoints, but when the user navigates back to the Mainpage i dont see the changes reflected in the listview at all. Also want to mention I am not following the MVVM architecture, since I was not sure how to implement it.

    Here is my class:

    public class Schedule : INotifyPropertyChanged { [PrimaryKey, AutoIncrement] public int Id { get; set; } private DateTime date; public DateTime Date { get { return date; } set { if (value != this.date) { date = value; NotifyPropertyChanged("Date"); } } } private DateTime shiftstart; public DateTime ShiftStart { get { return shiftstart; } set { if (value != this.shiftstart) { shiftstart = value; NotifyPropertyChanged("ShiftStart"); } } } private DateTime shiftend; public DateTime ShiftEnd { get { return shiftend; } set { if (value != this.shiftend) { shiftend = value; NotifyPropertyChanged("ShiftEnd"); } } } private string shiftnotes; public string ShiftNotes { get { return shiftnotes; } set { if (value != this.shiftnotes) { shiftnotes = value; NotifyPropertyChanged("ShiftNotes"); } } } private string company; public string Company { get { return company; } set { if (value != this.company) { company = value; NotifyPropertyChanged("Company"); } } } private string color; public string Color { get { return color; } set { if (value != this.color) { color = value; NotifyPropertyChanged("Color"); } } } private string visibility; public string Visibility { get { return visibility; } set { if (value != this.visibility) { visibility = value; NotifyPropertyChanged("Visibility"); } } } }

    Here is my Observable collection declaration:

    public ObservableCollection<Schedule> theSchedulesOC { get; set; }
    

    This is how I add records to the collection:

    private async void populateDates(DateTime theWeek)
            {
                theSchedulesOC = new ObservableCollection<Schedule>();
                for (int i = 0; i < 7; i++)
                {
                    try
                    {
                        var existing = await App.db.QueryAsync<Schedule>("select * from Schedule where Date = ? order by ShiftStart", theWeek.AddDays(i));
                        if (existing.Count != 0)
                        {
                            foreach (var s in existing)
                                theSchedulesOC.Add(s);
                        }
                        else
                        {
                            Schedule theSchedule = new Schedule { Date = theWeek.AddDays(i) };
                            theSchedulesOC.Add(theSchedule);
                        }
                    }
                    catch { }
                }
                populateListView(myPivot.SelectedIndex);
            }
     private void populateListView(int selectedPivot)
            {
                switch (selectedPivot)
                {
                    case 0:
                        lvLeft.ItemsSource = theSchedulesOC;
           
                        lvCenter.ItemsSource = null; // Solving listview flicker issues.
                        lvRight.ItemsSource = null; // """"""""
                        break;
                    case 1:
                        lvCenter.ItemsSource = theSchedulesOC;
                        lvLeft.ItemsSource = null; // Solving listview flicker issues.
                        lvRight.ItemsSource = null; // """"""""
                        break;
                    case 2:
                        lvRight.ItemsSource = theSchedulesOC;
                        lvLeft.ItemsSource = null; // Solving listview flicker issues.
                        lvCenter.ItemsSource = null; // """"""""
                        break;
                }
                return;
            }

    I am not sure how trigger the collection changed event of the Observable collection.

    Please advise.




    Thanks, Sumesh

    Wednesday, June 17, 2015 4:08 PM

Answers

  • Because the async void will return when you hit the first await, which is too early. The task can be awaited.

    https://msdn.microsoft.com/en-us/library/hh191443.aspx

    Saturday, June 20, 2015 4:07 PM

All replies

  • I suggest you start working on MVVM basics. There are several mistakes you are making in your code. it is better to just start from good foundations.

    https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

    Wednesday, June 17, 2015 5:06 PM
  • Would there be another way to do this at this point?

    Thanks, Sumesh

    Wednesday, June 17, 2015 5:34 PM
  • If you want to handle ObservableCollection Changed event, like this:

    theSchedulesOC.CollectionChanged += theSchedulesOC_CollectionChanged;
    
    void theSchedulesOC_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // Handle here
    }
    


    Best Regards,
    Please remember to mark the replies as answers if they help

    Thursday, June 18, 2015 8:53 AM
  • I tried that, for some reason that event wont fire.

    Thanks, Sumesh

    Thursday, June 18, 2015 11:12 PM
  • That populateListView method looks rather strange to me.

    The populateDates method is also wrong. You need to return async Task not async void, and you have to await its execution. You should also just create 3 observable collections (one for each pivot) and then use those 3 all the time. Do not recreate them using the new operator. Also make the global variables in your class and not local.

    Try setting the item sources only once, and see how that works out for you. Also do it in the constructor, to make sure it is called each time the page is created. The so called flicker listview issue is caused by binding too many items to it without proper virtualization. 

    That is an advanced topic and we shall work it out once you have finished the basic stuff.



    • Edited by mcosmin Friday, June 19, 2015 7:23 AM
    Friday, June 19, 2015 7:21 AM
  • Thanks mscosmin for the detailed explanation. Just confused with the async Task and async void. Why should I change it to async Task.


    Thanks, Sumesh

    Friday, June 19, 2015 9:44 PM
  • Because the async void will return when you hit the first await, which is too early. The task can be awaited.

    https://msdn.microsoft.com/en-us/library/hh191443.aspx

    Saturday, June 20, 2015 4:07 PM
  • Thanks mscosmin. 

    Thanks, Sumesh

    Saturday, June 20, 2015 5:36 PM