簡體   English   中英

訂閱財產變更事件

[英]Subscribing to Property Changed Events

我有一個傳感器班和一個測試班。 每個測試對象都有一組傳感器。 我的MainWindow類具有一個Test Object。 傳感器類擴展了INotifyPropertyChanged,並且我設置了一個Event以在某些屬性更改時廣播。 我的問題是,我不知道如何訂閱MainWindow類中的那些事件。 MainWindow裝有Chromium嵌入式窗口,包裝在CefSharp中。 我沒有需要更改的UI元素,我只需要在事件發生時調用函數/方法即可。

這是我目前正在嘗試的操作,但是在運算符的右側不允許不斷出現有關該屬性的錯誤?

傳感器等級

//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);
    }
}

在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);
}

我看到的所有示例都是在WPF端為按鈕或其他東西設置這些,並綁定到類中的數據。 我想在傳感器的數據數組發生任何更改時僅觸發MainWindow類中的方法。

在此先感謝您的幫助!

我想到了。 我必須在Sensor類中分配希望Event調用的函數。 這是我的新代碼

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

其中, handleStuff是在MainWindow類中某個位置定義的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM