写委托方法的问题

时间:2011-09-27 12:59:13

标签: c#-4.0 delegates action handler

我在向EventHandler添加参数时遇到问题。 当用户选择消息时我有一个控件,并选择他想要发送的人。

我需要处理OnConfirmForwarClosed以某种方式添加变量项。

我该怎么做?

private void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            var item = (RadMenuItem)e.Source;
            RadWindow.Confirm("Do you want forward this message to item.DataContext.HandlerName ?", OnConfirmForwarClosed);


        }

        private void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
         //here I need item from caller
            }
        }

修改 我需要一个可以通过这个解决方案得到的结果:

 RadWindow.Confirm("Do you want forward this message to item.DataContext.HandlerName ?",(s,ea)=>
                                         {
                                             if(ea.DialogResult==true)
                                             {
                                                 MessageBox.Show((item.DataContext as Handler).HandlerId.ToString());
                                             }
                                         });

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你不想将局部变量“提升”为lambda,例如......

void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
    var item = (RadMenuItem)e.Source;
    RadWindow.Confirm(
        "Do you want forward this message to item.DataContext.HandlerName ?",
        (sender, e) => {
            if (e.DialogResult == true) {
                // You can use 'item' here directly.
            }
        }
    );
}

...相反,你想保留OnConfirmForwarClosed(可能是为了订阅此处未显示的事件)。如果这是正确的,那么你有几个选择:

1

你可以用不同的方式安排你的方法:

void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
    var item = (RadMenuItem)e.Source;
    RadWindow.Confirm(
        "Do you want forward this message to item.DataContext.HandlerName ?",
        (sender, e) => OnConfirmForwarClosedImp(sender, e, item)
    );
}

void OnConfirmForwarClosedImp(object sender, WindowClosedEventArgs e, RadMenuItem item) {
    if (e.DialogResult == true) {
        if (item != null) {
            // Use 'item'.
        }
        else {
            // OnConfirmForwarClosed was called from somewhere else.
        }
    }
}

void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e) {
    OnConfirmForwarClosedImp(sender, e, null);
}

2

设置对象字段:

void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
    item = (RadMenuItem)e.Source;
    try {
        RadWindow.Confirm(
            "Do you want forward this message to item.DataContext.HandlerName ?",
            OnConfirmForwarClosed
        );
    }
    finally {
        item = null;
    }
}

RadMenuItem item = (RadMenuItem)e.Source;

void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e) {
    if (e.DialogResult == true) {
        if (item != null) {
            // Use 'this.item'.
        }
        else {
            // OnConfirmForwarClosed was called from somewhere else.
        }
    }
}

3

选择其中一个现有的OnConfirmForwarClosed参数:

void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
    var item = (RadMenuItem)e.Source;
    RadWindow.Confirm(
        "Do you want forward this message to item.DataContext.HandlerName ?",
        (sender, e) => OnConfirmForwarClosed(item, e)
    );
}

void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e) {
    if (e.DialogResult == true) {
        var item = sender as RadMenuItem;
        if (item != null) {
            // Use 'item'.
        }
        else {
            // OnConfirmForwarClosed was called from somewhere else.
        }
    }
}

Etc等......