0

I have an interface for a UI widget, two of which are attributes of a presenter.

public IMatrixWidget NonProjectActivityMatrix {
        set {
            // validate the incoming value and set the field
            _nonProjectActivityMatrix = value;
            ....
            // configure & load non-project activities
     }

public IMatrixWidget ProjectActivityMatrix {
        set {
            // validate the incoming value and set the field
            _projectActivityMatrix = value;
            ....
            // configure & load project activities
   }

The widget has an event that both presenter objects subscribe to, and so there is an event handler in the presenter like so:

public void OnActivityEntry(object sender, EntryChangedEventArgs e) {
    // calculate newTotal here
    ....
        if (ReferenceEquals(sender, _nonProjectActivityMatrix)) {
            _nonProjectActivityMatrix.UpdateTotalHours(feedback.ActivityTotal);
        }

        else if (ReferenceEquals(sender, _projectActivityMatrix)) {
            _projectActivityMatrix.UpdateTotalHours(feedback.ActivityTotal);
        }
        else {
            // ERROR - we should never be here
        }
    }

The problem is that the ReferenceEquals on the sender fails, even though it is the implemented widget that is the sender - the same implemented widget that was set to the presenter attribute!

Can anyone spot what the problem / fix is?

Cheers, Berryl

I didn't know you could edit nicely. Cool. Here is the event raising code:

void OnGridViewNumericUpDownEditingControl_ValueChanged(object sender, EventArgs e)
    {
    // omitted to save sapce

        if (EntryChanged == null) return;

        var args = new EntryChangedEventArgs(activityID, dayID, Convert.ToDouble(amount));
        EntryChanged(this, args);
    }

Here is the debugger dump of the presenter attribute, sans namespace info:

?_nonProjectActivityMatrix
{WinPresentation.Widgets.MatrixWidgetDgv}
[WinPresentation.Widgets.MatrixWidgetDgv]: {WinPresentation.Widgets.MatrixWidgetDgv}

Here is the debugger dump of the sender:

?sender
{WinPresentation.Widgets.MatrixWidgetDgv}
base {Core.GUI.Widgets.Lookup.MatrixWidgetBase<Core.GUI.Widgets.Lookup.DynamicDisplayDto>}: {WinPresentation.Widgets.MatrixWidgetDgv}
_configuration: {Domain.Presentation.Timesheet.Matrix.WeeklyMatrixConfiguration}
_wrappedWidget: {Win.Widgets.DataGridViewDynamicLookupWidget}
AllowUserToAddRows: true
ColumnCount: 11
Count: 4
EntryChanged: {Method = {Void OnActivityEntry(System.Object, Smack.ConstructionAdmin.Domain.Presentation.Timesheet.Matrix.EntryChangedEventArgs)}}
SelectedCell: {DataGridViewNumericUpDownCell { ColumnIndex=3, RowIndex=3 }}
SelectedCellValue: "0.00"
SelectedColumn: {DataGridViewNumericUpDownColumn { Name=MONDAY, Index=3 }}
SelectedItem: {'AdministrativeActivity: 130-04', , AdministrativeTime, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}
  • Berryl
4

1 回答 1

0

在我的脑海中,我可以想到 ReferenceEquals 将返回 false 的以下原因:

  • senderis -在引发事件时null验证小部件是否作为 sender 参数发送。this
  • 由于某种原因,小部件没有分配给演示者,所以它是null
  • sender不是您认为的那样(您如何验证它确实是您期望的发件人)
  • 小部件实现为 a struct,而不是 aclass

可能还有更多,但这是我要开始的地方(按此顺序)。

更新:引起我注意的一件事是:

_wrappedWidget: {Win.Widgets.DataGridViewDynamicLookupWidget}

可能是 sender 是一个包装你的小部件的对象吗?如果是这种情况,ReferenceEquals 在进行比较时自然会返回 false。

于 2009-06-06T16:42:01.603 回答