Question

I have a Sensor Class and a Test Class. Each Test Object has an array of Sensors. My MainWindow class has a Test Object. The Sensor Class extends INotifyPropertyChanged and I have an Event set up to broadcast when a certain property changes. My problem is, I do not know how to subscribe to those events in the MainWindow Class. The MainWindow holds a Chromium Embedded windows, wrapped in CefSharp. I do not have a UI element that needs to change, I just need to call a function/method whenever an event occurs.

This is what I am currently trying, but keep getting an error about the property is not allowed on the right side of the operator?

Sensor Class

//Event for when new data is placed into temp_readings
public event PropertyChangedEventHandler PropertyChanged;

//Adds a new reading to the data set
public void addReading(float reading)
{
    this.temp_readings.Add(reading);
    OnPropertyChanged(new PropertyChangedEventArgs("new_data_id" + this.id));
}

//Raises an event that new readings have been added
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, e);
    }
}

In MainWindow

private void InitializeWebView()
{
    //Disable Caching
    BrowserSettings settings = new BrowserSettings();
    settings.ApplicationCacheDisabled = true;
    settings.PageCacheDisabled = true;
    settings.FileAccessFromFileUrlsAllowed = true;

    //Initialize WebView
    this.webView = new WebView(index, settings);

    //View Property Event Handlers
    this.webView.PropertyChanged += this.webViewPropertyChanged;

    //Event handlers for new data added to sensors
    for (int x = 0; x < this.test.sensors.Length; x++)
    {
        this.webView.PropertyChanged += this.test.sensors[x].PropertyChanged;
    }

    //Load it into the XAML Grid
    main_grid.Children.Add(webView);
}

All of the examples I see are setting these up for buttons or something in the WPF side, and binding to data in a class. I'm wanting to just fire off a method in the MainWindow Class whenever anything changes to a sensor's data array.

Thanks in advance for any help!

Was it helpful?

Solution

I figured it out. I had to assign what function I wanted the Event in the Sensor class to call. This is my new code

//Event handlers for new data added to sensors
for (int x = 0; x < this.test.sensors.Length; x++)
{
    this.test.sensors[x].PropertyChanged += handleStuff;
}

Where, handleStuff is a function defined in somewhere in the MainWindow Class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top