简体   繁体   English

通过作为参数传递的委托来操作对象的字段

[英]Manipulate object's fields by a delegate passed as parameter

I'm crating a custom dialog class. 我正在创建一个自定义对话框类。 It takes a list of OptionObject objects, in constructor. 它在构造函数中采用OptionObject对象的列表。 At some point it's displayed and depending on which option the user clicks performs an action that is injected into OptionObject. 在某个时候它会显示出来,并且取决于用户单击哪个选项,将执行注入到OptionObject中的操作。

public class OptionObject
    {
        public object Id { get; set; }
        public string Text { get; set; }

        public Action Action { get; set; }

        public OptionObject(object id, string text, Action action)
        {
            this.Id = id;
            this.Text = text;
            this.Action = action;
        } 
}

This is how the actions are invoked by DialogWindow: 这是DialogWindow调用动作的方式:

this.Options.Tap += (sender, args) =>   // Options is a ListBox
                                    {
                                        var o = sender as ListBox;
                                        var x = o.SelectedItem as OptionObject;
                                        if(x!=null)
                                            x.Action();
                                        this.IsOpen = false;
                                    };

And finally I'm trying to use my dialog on another page: 最后,我尝试在另一页上使用我的对话框:

foreach (var o in eventsOnDate)
                {
                    chooseEventDialogWindow.Data.OptionsList.Add(new OptionObject(o.Text, ()=> /*### don't know what to put in here ###*/ ));
                }

The problem is that OptionObject.Action sometimes needs OptionObject.Id property. 问题在于OptionObject.Action有时需要OptionObject.Id属性。 How can I access it in the sniped right above? 如何在上面的截图中访问它?

EDIT 编辑

I wouldn't like to pass any parameters into this lambda expression if it's possible, as sometime there's no need for this Id. 如果可能的话,我不想将任何参数传递到此lambda表达式中,因为有时不需要此ID。 Optional parameters would be fine if this is the right way to go. 如果这是正确的方法,则可选参数会很好。

PS 聚苯乙烯

This is not important for the question but to avoid unnecessary discussion this dialog is based on telerik's RadWindow. 这对于该问题并不重要,但是为了避免不必要的讨论,此对话框基于telerik的RadWindow。

An Action is a method that has no parameters and does not return a value, so you either have to enclose your OptionObject.ID property when you create the Action delegate: Action是没有参数且不返回值的方法,因此在创建Action委托时,您必须封装OptionObject.ID属性:

object id = new object();
Action act = () => DoSomethingWithIdAndReturnVoid(id);
OptionObject opt = new OptionObject(id, "some text", act);
opt.Action();

Or do like Henk suggests in his comment, ie, create an Action that has an input parameter : 或者像Henk在评论中建议的那样做,即创建一个具有输入参数Action

object id = new object();
Action<object> act = (x) => DoSomethingWithIdAndReturnVoid(x);
OptionObject opt = new OptionObject(id, "some text", act);
opt.Action(opt.ID);

Just be aware that changes to enclosed variables only work properly when dealing with Reference Types . 要知道,改变封闭的变量与打交道时,只有正常工作引用类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM