如何使用UI映射对象 [英] How to map objects with UI

查看:146
本文介绍了如何使用UI映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程实现(不完整,只是想提出一个想法)

I have following class implementation (not complete, just to give an idea)

   public class MySwitch{
        Command command = Command.Red;
        public Command GetNext() {
            command = GetNext(command); // circular enum values
            return command;
        }
    }


public enum Command { Red =0, Blue=1, Green=2}

public class LED {
    public void Glow(Command command){
        this.setColor(ColorForm(command));
        this.Glow();
    }
}

public class Commander {
    public Commander(LED target, MySwitch source){
        this.LED = LED;
        this.MySwitch = MySwitch;
    }

    public void Execute(){
        this.LED.Glow(this.MySwitch.GetNext());
    }
}

我希望这些对象映射到UI项。考虑一下,我有赢表应用程序,其中开关和LED是两个面板,我希望GDI绘制它。

I want these objects to map to UI items. Consider, I have win form app where switch and LED are two panel where I want GDI to draw it.

问题是同步带有UI元素的对象的最佳方法。选项为:

Issue is what is best way sync objects with UI elements. Options are:


  1. 创建随面板继承的UI元素,并且应包含一个
    对象实例。

  1. Create UI element inherited with panel and should contains one instance of object.

创建从BO继承的UI元素(例如LEDUI),并且
应该包含容器(面板)以绘制并使用this来实现绘制方法。颜色
(例如,LED)-这将导致文件2 * BO

Create UI element (say LEDUI) inherited from BO, and should contain container (panel) to draw and implement draw method using this.Color (LED for example) - this will lead to file cound 2* BO

保持UI元素和BO分开,并让演示者在
之间建立桥梁

Keep UI element and BO separate and let presenter to be bridge between them.

BO本身的实现方法,以在winform上呈现(假定单个UI)。由于
不能直接添加到winform中,因此创建一个 CustomForm
对象,该对象允许使用此类元素(假设 IMyObj ),然后
调用 CustomFOrm.Render(),最终调用所有子元素
的渲染方法。

Implement method on BO itself to render (assuming single UI) on winform. Since it cannot be added to winform directly, so create a CustomForm object which allows such elements (assume IMyObj) to be added, and call CustomFOrm.Render(), which eventually call render method of all childElements. Pretty same way Form and controls are rendered.

其他方法

在我看来,第2点是更好的方法。请提出将BO与UI映射的不同方式的利弊,以及如何同步它们。游戏开发人员可能会有更好的理解。

In my opinion, point 2 is better way. Please suggest what pros and cons on different way of mapping BO with UI, and how to sync them. Game developers may have better understanding.

编辑

我的错误可能是有许多LED和开关。每个开关可以连接到许多LED。我创建的类也独立于UI。我不期望找到如何找到控件和辉光的解决方案,但是如果给了您这些类并被告知制作一个Winform应用,假设您最少接触这些类并编写最少的代码,那么实现的最佳方法是什么? ,以及以下标准的UI开发方式

My mistake, there could be many LED and switches. Each switch may be attached to many LED. Also the classes I have created is independent of UI. I don't expect solution to how to find the control and glow, but what is the best way to implement if you are given these classes and told to make a winform app, assuming u will least touch the classes as well as write minimum code, along with following standard way of UI development

推荐答案

在C#(WinForms)中,每个UI元素都有一个 Tag 成员,您可以根据需要添加任何内容。因此,当用户按下开关时,代码可以遍历控件,以查找具有与当前选择匹配的 Tag 的控件。

In C# (well, WinForms) each UI element has a Tag member into which you can put whatever you want. So, when the user presses the 'switch', the code can iterate over the controls looking for controls with a Tag that matches the current selection.

例如,假设LED图像的 Tag 字段中有文本 LED =,那么您可以编写一个函数来设置发光状态:

For example, suppose the LED images have the text 'LED=' in their Tag field, then you can write a function to set the glow state:

// example usage: SetGlow ("Red");

void SetGlow (string colour)
{
   SetGlow (Controls, colour);
}

void SetGlow (ControlContainer controls, string colour)
{
  foreach (Control control in controls)
  {
    if (control.Tag is a string) // syntax escapes me
    {
      string tag = (string) control.Tag;
      if (tag.StartsWith ("LED="))
      {
        if (tag == ("LED=" + colour))
        {
          // enable glow
        }
        else
        {
          // disable glow
        }
      }
    }

    SetGlow (control.Controls, colour);
  }
}

这篇关于如何使用UI映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆