Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# INotifyPropertyChanged(如何订阅事件以及为什么eventhandler始终为空)_C#_Event Handling_Inotifypropertychanged - Fatal编程技术网

C# INotifyPropertyChanged(如何订阅事件以及为什么eventhandler始终为空)

C# INotifyPropertyChanged(如何订阅事件以及为什么eventhandler始终为空),c#,event-handling,inotifypropertychanged,C#,Event Handling,Inotifypropertychanged,为什么eventHandler总是空的。我将抽象单元格中的sCell Inheriting的每个实例订阅到事件中,但它仍然保持为空。在SpreedSheet构造函数中,去掉以下行: public abstract class Cell : INotifyPropertyChanged { protected string text; protected string value; readonly int rowIndex; readonly int column

为什么eventHandler总是空的。我将抽象单元格中的sCell Inheriting的每个实例订阅到事件中,但它仍然保持为空。

在SpreedSheet构造函数中,去掉以下行:

public abstract class Cell : INotifyPropertyChanged
{
    protected string text;
    protected string value;
    readonly int rowIndex;
    readonly int columnIndex;

    public Cell()
    {
    }

    public Cell(int ri, int ci)
    {
        rowIndex = ri;
        columnIndex = ci;
    }

    public Cell CreateCell()
    {
        return this;
    }

    public string Value
    {
        get { return value; }
    }

    public int RowIndex 
    {
        get { return rowIndex; }
    }

    public int ColumnIndex
    {
        get { return columnIndex; }
    }



    public string Text
    {
        get
        {
            return text;
        }
        set
        {
            text = value;

            this.OnPropertyChanged("text");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // Create the OnPropertyChanged method to raise the event 
    protected virtual void OnPropertyChanged(string propertyName = "")
    {
        var eventHandler = this.PropertyChanged;

        if (eventHandler != null)
        {
            eventHandler.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

public class SpreadSheet
    {
        private sCell[,] cellGrid;
        private int rows, cols;
    private int RowCount
    {
        get { return rows; }
    }

    private int ColumnCount
    {
        get { return cols; }
    }

    private class sCell : Cell
    {
        public sCell(int ri, int ci) : base(ri, ci)
        {
        }

        public static sCell createCell(int ri, int ci)
        {
            return new sCell(ri, ci);
        }

        public void setValue(sCell[,] cellGrid)
        {
            string thisText = this.Text;

            if (thisText[0] == '=')
            {
                byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(thisText);

                this.value = cellGrid[asciiBytes[1] - 65, asciiBytes[2]].Value;
            }
            else
            {
                this.value = thisText;
            }

        }

    }

    public event PropertyChangedEventHandler CellPropertyChanged;

    public SpreadSheet(int numRows, int numColumns)
    {

        PropertyChangedEventHandler handler = CellPropertyChanged;

        cellGrid = new sCell[numRows, numColumns];

        rows = numRows;
        cols = numColumns;

        int i, j;

        for (i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                sCell newCell = sCell.createCell(i, j);
                cellGrid[i, j] = newCell;
                newCell.PropertyChanged += handler;
            }
        }
    }

    public Cell getCell(int ri, int ci)
    {
        if (cellGrid[ri, ci] != null)
        {
            return cellGrid[ri, ci];
        }
        else
        {
            return null;
        }

    }

    protected void OnCellPropertyChanged(Cell c, string text)
    {
        PropertyChangedEventHandler handler = CellPropertyChanged;

        if (handler != null)
        {
            handler(c, new PropertyChangedEventArgs(text));
        }
    }

    private void handler(object sender, PropertyChangedEventArgs e)
    {
        sCell c = sender as sCell;

        c.setValue(this.cellGrid);

        OnCellPropertyChanged(sender as Cell, "CellValue");
    }

}

看起来它应该正确订阅,这覆盖了电子表格.handler方法。

您是用另一个事件订阅一个事件吗?e、 g.您将处理程序设置为Speedsheet.CellPropertyChanged事件,然后使用处理程序分配递增的Cell.PropertyChanged?您没有初始化处理程序事件变量
PropertyChangedEventHandler handler = CellPropertyChanged;