简体   繁体   English

订阅财产变更事件

[英]Subscribing to Property Changed Events

I have a Sensor Class and a Test Class. 我有一个传感器班和一个测试班。 Each Test Object has an array of Sensors. 每个测试对象都有一组传感器。 My MainWindow class has a Test Object. 我的MainWindow类具有一个Test Object。 The Sensor Class extends INotifyPropertyChanged and I have an Event set up to broadcast when a certain property changes. 传感器类扩展了INotifyPropertyChanged,并且我设置了一个Event以在某些属性更改时广播。 My problem is, I do not know how to subscribe to those events in the MainWindow Class. 我的问题是,我不知道如何订阅MainWindow类中的那些事件。 The MainWindow holds a Chromium Embedded windows, wrapped in CefSharp. MainWindow装有Chromium嵌入式窗口,包装在CefSharp中。 I do not have a UI element that needs to change, I just need to call a function/method whenever an event occurs. 我没有需要更改的UI元素,我只需要在事件发生时调用函数/方法即可。

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 在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. 我看到的所有示例都是在WPF端为按钮或其他东西设置这些,并绑定到类中的数据。 I'm wanting to just fire off a method in the MainWindow Class whenever anything changes to a sensor's data array. 我想在传感器的数据数组发生任何更改时仅触发MainWindow类中的方法。

Thanks in advance for any help! 在此先感谢您的帮助!

I figured it out. 我想到了。 I had to assign what function I wanted the Event in the Sensor class to call. 我必须在Sensor类中分配希望Event调用的函数。 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. 其中, handleStuff是在MainWindow类中某个位置定义的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM