0

Visual Studio 2010 の WPF プロパティ グリッドの非常に奇妙な動作に遭遇しました。プロパティとイベント ハンドラーの大規模なセットを備えたコンポーネント ツールセットを開発しています。refパラメータが問題を引き起こしています。

私のオブジェクトの 1 つで、次のように定義されたrefPositionChangedパラメーターを持つイベント ハンドラーがあります。

public delegate void PositionChangedHandler(LineSeriesCursor sender, double newValue, **ref** bool cancelRendering); 

public event PositionChangedHandler PositionChanged = null;

のインスタンスを作成し、LineSeriesCursorイベント ハンドラーを定義するとき

LineSeriesCursor cursor = new LineSeriesCursor(); 
cursor.PositionChanged += [TAB][TAB]

ハンドラー メソッド スタブを正しく作成します。

cursor.PositionChanged += new LineSeriesCursor.PositionChangedHandler(cursor_PositionChanged);
void cursor_PositionChanged(LineSeriesCursor sender, double newValue, **ref** bool cancelRendering)
{
           //this works and compiles then OK. 
}

ただし、LineSeriesCursorWPF プロパティ グリッドにを追加してからLineSeriesCursorXAML のタグに移動しPositionChanged、プロパティ グリッドからイベント ハンドラーを追加すると、次のようにメソッド スタブがrefなしで作成されます。

private void LineSeriesCursor_PositionChanged(LineSeriesCursor sender, double newValue, bool cancelRendering)
{
    //Does not compile, because of invalid method parameter list. **ref** is missing.
}

Visual Studio 2010 のバグのように思えます。この件に関する同様の経験やアドバイスはありますか?

助けてくれてありがとう。

4

1 に答える 1

0

マイクロソフトのやり方に従って、 EventArgsから派生してみませんか

例えば

public class PositionChangedEventArgs : EventArgs
{
   public bool Cancel {get;set;}
   public int NewValue {get;set;}

} 

public event EventHandler<PositionChangedEventArgs> PositionChanged {get;set;}


protected virtual void OnPositionChanged(int newValue)
{
    if (this.PositionChanged !=null)
    {
        var args = new PositionChangedEventArgs(){NewValue = newValue};
        this.PositionChanged(this,args);
        if (args.Cancel)
        {
            //Do something to cancel..
        }
    }
}

それで

cursor.PositionChanged += (sender,e) =>
{
    e.Cancel = true;
    var x = e.NewValue;
};
于 2013-01-31T11:43:39.923 に答える