简体   繁体   English

如何处理大量单独的 ui 命令 c#,如何获取、初始化和执行它们

[英]how to handle lots of separate ui commands c#, how to get them, initialize them and execute them

I have fairly complicated ui system that i want to simplify.我有相当复杂的 ui 系统,我想简化它。 What I want is to make separate classes with command and fluent builder pattern that which instances I will create at the beginning of the app and then execute when needed, something like this:我想要的是使用命令和流利的构建器模式创建单独的类,我将在应用程序开始时创建哪些实例,然后在需要时执行,如下所示:

public interface IGuiCommand
{
  void Execute();
  void Execute(Action onComplete = null);
}

public class GuiCommandBuilder : IGuiCommand
{
  public void Execute()
  {

  }

  public void Execute(Action onComplete = null)
  {
  }
}

public class GuiCommandTest : GuiCommandBuilder
{
  private int _id;
        
  public GuiCommandTest()
  {
            
  }

  public GuiCommandTest Setup(int id)
  {
    _id = id;
    return this;
  }
}

so I can do this:所以我可以这样做:

// create command
var GuiRoomSelectionCommand = new GuiCommandTest();

// setup and execute (I have to provide additional data in order
// command to work correctly, some commands don't need additional data)
GuiRoomSelectionCommand
  .Setup(223)
  .Execute(() =>
  {

  });

The problem I am having is that I want to create all commands at the start of the app and store them for later easy access, but I can't just store them as IGuiCommand since I will need a concrete instance in order to setup the concrete commands?我遇到的问题是我想在应用程序开始时创建所有命令并将它们存储起来以便以后轻松访问,但我不能将它们存储为 IGuiCommand 因为我需要一个具体实例来设置具体命令? How to do that, or if any other setup is better approach?如何做到这一点,或者是否有任何其他设置是更好的方法?

I want that others can easily see what commands are there and how to add more of them if needed or easily modify the existing ones in separate classes so that main controller doesn't change to much but only specific commands我希望其他人可以轻松查看那里有哪些命令以及如何在需要时添加更多命令,或者轻松修改单独类中的现有命令,以便主控制器不会更改太多,而只会更改特定命令

Define an ICommandParameter interface to indicate the name of the parameter and its current value.定义一个ICommandParameter接口来指示参数的名称及其当前值。

You can create different types of parameters by implementing this interface:您可以通过实现此接口来创建不同类型的参数:

public ICommandParameter
{
    string Name {get;set;}
    object ObjectValue {get;set;}
}

public IDoubleParameter : ICommandParameter
{
    double MinValue {get;set;}
    double MaxValue {get;set;}
    double Number {get;set;}
    int DecimalsCount {get;set;}
}

public IBoolParameter : ICommandParameter
{
    bool Checked {get;set;}
}

public IAnimalsParameter : ICommandParameter
{
    Animal Animal {get;set;} // Animal is an enum with a list of values
}

Now, your IGuiCommand can have a list of ICommandParameter that you can initialize in the constructor of your command.现在,您的IGuiCommand可以有一个ICommandParameter列表,您可以在命令的构造函数中对其进行初始化。

In GuiCommandBuilder (or in other class if you consider) you can manage the parameters.GuiCommandBuilder (或其他类,如果你考虑)你可以管理参数。 You have access to the command parameters and show/update using some Forms.您可以使用某些表单访问命令参数和显示/更新。

For example, in your GuiRoomSelectionCommand you can access to the parameters list, with a simgle parameter "Number of rooms" that you can see (you can check/try cast) that implements IDoubleParameter with this values:例如,在您的GuiRoomSelectionCommand中,您可以访问参数列表,使用您可以看到的简单参数“房间数”(您可以检查/尝试演员表)使用以下值实现IDoubleParameter

Name = "Number of rooms"
ObjectValue = 2
MinValue = 1
MaxValue = 10
Number = 2 // Usually a cast of ObjectValue interface
DecimalsCount = 0

You can create a Form in which you add some controls depending of parameters type.您可以创建一个表单,根据参数类型在其中添加一些控件。 For previous example, you can add a Label for Name and a NumericUpDown for the value (settings decimals, minimum and maximum).对于前面的示例,您可以为Name添加一个Label ,为值添加一个NumericUpDown (设置小数、最小值和最大值)。 You initialize the numeric with the Number property.使用Number属性初始化数字。 And when you click Ok in the dialog, you can update de Number property with the numeric value.当您在对话框中单击 Ok 时,您可以使用数值更新 de Number属性。

For any Enum property, you can add a ComboBox to show/edit it's value.对于任何 Enum 属性,您可以添加一个 ComboBox 来显示/编辑它的值。 For a bool property, a CheckBox and so on.对于bool属性、 CheckBox等。

You need spend some time to create the controls for any type of property to manage but, at the end, most of properies are of basic types.您需要花一些时间来为要管理的任何类型的属性创建控件,但最终,大多数属性都是基本类型。 There aren't lots of controls.没有太多的控制。

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

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