从ListView

时间:2017-09-18 04:15:10

标签: c#

我正在尝试创建一组允许我创建应用程序将执行的“命令树”的类。这些命令最初放入Details List View,然后通过for each循环解析(命令分配给.Tag属性),然后添加到执行脚本中,该脚本将被序列化为XML。这是我的意思的一个逻辑例子:

        EXAMPLE: Expected Input from ListView
        >Command - Do Something (Line 1)
        >Command - Do Something (Line 2)
        >Command - Loop (Line 3)
        >Command - Do Something (Line 4)
        >Command - Do Something (Line 5)
        >Command - End Loop (Line 6)
        >Command - Do Something (Line 7)

        EXAMPLE: Expected Class Output (For Serialization)
        >Command - Do Something (Line 1)
        >Command - Do Something (Line 2)
        >Command - Loop (Line 3)
                >Command - Do Something (Line 4) (Child to Line 3)
                >Command - Do Something (Line 5) (Child to Line 3)
        >Command - End Loop (Line 6)
        >Command - Do Something (Line 7)

        EXAMPLE: Expected Input from ListView
        >Command - Loop (Line 1)
        >Command - Do Something (Line 2)
        >Command - Loop (Line 3)
        >Command - Do Something (Line 4)
        >Command - End Loop (Line 5)
        >Command - End Loop (Line 6)

        EXAMPLE: Expected Class Output (For Serialization)
        >Command - Loop (Line 1)
                >Command - Do Something (Line 2) (Child to Line 1)
                >Command - Loop (Line 3) (Child to Line 1)
                        >Command - Do Something (Line 4) (Child to Line 3)
                >Command - End Loop (Line 5) (Child to Line 1)
        >Command - End Loop (Line 6)

以下是我的代码:

public class ExecutionScript
{
//the Execution Script class will be serialized to XML
List<ExecutionCommand> ExecutionCommands;
public ExecutionScript()
{
    ExecutionCommands = new List<ExecutionCommand>();
}
public ExecutionCommand AddNewParentCommand(ExecutionInfo executionInfo)
{
    ExecutionCommand newExecutionCommand = new ExecutionCommand() { ParentCommand = executionInfo };
    ExecutionCommands.Add(newExecutionCommand);
    return newExecutionCommand;
}
}
public class ExecutionInfo
{
//the Execution Info class contains information about the command
public string CommandName { get; set; }
}

public class ExecutionCommand
{
//the Execution Command class helps expand together
public ExecutionInfo ParentCommand { get; set; }
public List<ExecutionCommand> ChildCommands { get; set; }
public ExecutionCommand()
{
    ChildCommands = new List<ExecutionCommand>();
}
public ExecutionCommand AddSubCommand(ExecutionInfo executionInfo)
{
    ExecutionCommand newExecutionCommand = new ExecutionCommand() { ParentCommand = executionInfo };
    ChildCommands.Add(newExecutionCommand);
    return newExecutionCommand;
}
}

类设置看起来有点笨拙和笨拙 - 可能有一种更聪明的方法来构建它。鉴于上面的示例,以下是我将如何手动实例化和设置类(这应该最终自动完成)。

       //because I know where to expect children:
        ExecutionScript newScript = new ExecutionScript();
        newScript.AddNewParentCommand(new ExecutionInfo() { CommandName = "Command (Line 1)" });
        newScript.AddNewParentCommand(new ExecutionInfo() { CommandName = "Command (Line 2)" });
         var loopCommand = newScript.AddNewParentCommand(new ExecutionInfo() { CommandName = "Loop (Line 3)" });
        loopCommand.AddSubCommand(new ExecutionInfo() { CommandName = "Command (Line 4)" });
        loopCommand.AddSubCommand(new ExecutionInfo() { CommandName = "Command (Line 5)" });
        newScript.AddNewParentCommand(new ExecutionInfo() { CommandName = "Command (Line 6)" });
        newScript.AddNewParentCommand(new ExecutionInfo() { CommandName = "Command (Line 7)" });

有没有办法简化类,使它成为一个比我更接近的树视图表示?我如何使用递归来构建ExecutionScript

1 个答案:

答案 0 :(得分:0)

也许复合模式在这里很有用

public static void Main()
    {
        ExecutionComposite composite = new ExecutionComposite(new ExecutionInfo() {CommandName = string.Empty});
        composite.Add(new ExecutionOne(new ExecutionInfo() {CommandName = "Command (Line 1)"}));
        composite.Add(new ExecutionOne(new ExecutionInfo() {CommandName = "Command (Line 2)"}));

        ExecutionComposite composite1 = new ExecutionComposite(new ExecutionInfo() {CommandName = "Loop (Line 3)"});
        composite.Add(composite1);
        composite1.Add(new ExecutionOne(new ExecutionInfo() {CommandName = "Command (Line 4)"}));
        composite1.Add(new ExecutionOne(new ExecutionInfo() {CommandName = "Command (Line 5)"}));

        ExecutionComposite composite2 = new ExecutionComposite(new ExecutionInfo() { CommandName = "Loop (Line 5)" });
        composite1.Add(composite2);
        composite2.Add(new ExecutionOne(new ExecutionInfo() { CommandName = "Command (Line 6)" }));
        composite2.Add(new ExecutionOne(new ExecutionInfo() { CommandName = "Command (Line 7)" }));

        composite.Add(new ExecutionOne(new ExecutionInfo() {CommandName = "Command (Line 8)"}));
        composite.Add(new ExecutionOne(new ExecutionInfo() {CommandName = "Command (Line 9)"}));

        composite.Print("--");
        Console.ReadLine();
    }
}

public interface IExection
{
    ExecutionInfo Info { get; }
    void Print(string indent);
}

public class ExecutionComposite : IExection
{
    List<IExection> executions = new List<IExection>();

    public ExecutionInfo Info { get; }

    public ExecutionComposite(ExecutionInfo executionInfo)
    {
        Info = executionInfo;
    }

    public void Add(IExection one)
    {
        executions.Add(one);
    }

    public void Remove(IExection one)
    {
        if (executions.Contains(one))
        {
            executions.Remove(one);
        }
    }

    public void Print(string indent)
    {
        indent += "\t";
        foreach (var exe in executions)
        {
            exe.Print(indent);
        }
    }
}

public class ExecutionOne : IExection
{
    public ExecutionInfo Info { get; }

    public ExecutionOne(ExecutionInfo executionInfo)
    {
        Info = executionInfo;
    }

    public void Print(string indent)
    {
        Console.WriteLine("{0}{1}", indent, Info.CommandName);
    }
}

public class ExecutionInfo
{
    //the Execution Info class contains information about the command
    public string CommandName { get; set; }
}