Home > Mobile >  How to fire the CollectionChangedEventHandler to read a CSV File
How to fire the CollectionChangedEventHandler to read a CSV File

Time:11-23

So i have this method to read from a CSV File and it works.

//Alte Bestand
    private void PageLoad_Alt(object sender, RoutedEventArgs e)
    {

        try
        {
            //Index von selectedItem
            string filepath = directory   "\\"   "ParkingTool_BESTAND.csv";


            //CSVHelper Config

            var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ";" };
            using (var csv = new CsvReader(new StreamReader(filepath), config))
            {
                csv.Read();
                csv.ReadHeader();

                while (csv.Read())
                {
                    string barcodeField = csv.GetField<string>("Barcode");
                    string boxField = csv.GetField<string>("BoxNr");
                    string datumField = csv.GetField<string>("Datum");
                    alte_parking_collection.Add(new ParkingClass() { parking_barcode = barcodeField, parking_box = boxField, parking_datum = datumField }); ;
                }
            }
            code_box.Focus();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Bitte Datei überprüfen! "   ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
            code_box.Focus();
        }
    }

Then i call it with Loaded = PageLoad_Alt; when the page starts.

But i want something that calls the method again when a another collection is edited. Any ideas?

parking_collection.CollectionChanged = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PageLoad_Alt());

This is not working.

CodePudding user response:

The issue is that Loaded is a routed event requiring RoutedEventArgs, while the CollectionChange event requires CollectionChangedEventArgs (which does not derive from RoutedEventArgs). So you can not use the same parameters. You don't appear to be using the event arguments anyway, so pull your code into a private method and call it from two event handlers.

private void PrintCSV()
{
     try
     {
         //Index von selectedItem
         string filepath = directory   "\\"   "ParkingTool_BESTAND.csv";


         //CSVHelper Config
         var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ";" };
         using (var csv = new CsvReader(new StreamReader(filepath), config))
         {
             csv.Read();
             csv.ReadHeader();

             while (csv.Read())
             {
                 string barcodeField = csv.GetField<string>("Barcode");
                 string boxField = csv.GetField<string>("BoxNr");
                 string datumField = csv.GetField<string>("Datum");
                 alte_parking_collection.Add(new ParkingClass() { parking_barcode = barcodeField, parking_box = boxField, parking_datum = datumField });
             }
      }
         code_box.Focus();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Bitte Datei überprüfen! "   ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
         code_box.Focus();
     }
}

private void PageLoad_Alt(object sender, RoutedEventArgs e) => PrintCSV();
private void OnCollectionChanged(object sender, CollectionChangedEventArgs e) => PrintCSV();

Then subscribe to the appropriate events

Loaded  = PageLoad_Alt;
parking_collection.CollectionChanged  = OnCollectionChanged;

CodePudding user response:

I am not sure what syntax you are using to attempt to call the method when the collection changes, but this will surely call your method when parking_collection changes:

parking_collection.CollectionChanged  = (s, e) => PageLoad_Alt(null, null);

Ensure that you register the above event handler on the current Collection that parking_collection references. For example, if at any point in your code you do something like this:

parking_collection = new ObservableCollection<Parking>();

you need to register the event handler again. You also might need to re-register when a Binding related to parking_collection changes if the collection is part of a binding. (Implementation specific but a common mistake)

  • Related