写委托方法的问题

发布于 12-07 08:02 字数 1198 浏览 0 评论 0原文

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

I have a problem with adding parameters to EventHandler.
I have a control when user select message and choose to who he want to send it.

I need to handler OnConfirmForwarClosed add in some way variable item.

How can I do it?

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
            }
        }

EDIT
I need a result which I can get by this solution:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你2024-12-14 08:02:18

如果我正确理解你的问题,你不想将局部变量“提升”到 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.
        }
    }
}

等等...

If i understand your question correctly, you don't want to "lift" the local variable into lambda, such as...

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

...and instead you want to keep the OnConfirmForwarClosed (presumably to subscribe to events not shown here). If that's correct, then you have couple of options:

1.

You could just arrange your methods differently:

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.

Set the object field:

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.

Co-opt one of the existing OnConfirmForwarClosed parameters:

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, etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文