[英]How to map objects with UI
我有以下 class 实现(不完整,只是为了给出一个想法)
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());
}
}
我希望这些对象到 map 到 UI 项。 考虑一下,我有一个 win 表单应用程序,其中开关和 LED 是我希望 GDI 绘制它的两个面板。
问题是将对象与 UI 元素同步的最佳方式。 选项是:
创建与面板继承的 UI 元素,并应包含 object 的一个实例。
创建从 BO 继承的 UI 元素(例如 LEDUI),并应包含容器(面板)以使用 this.Color(例如 LED)进行绘制和实现绘制方法 - 这将导致文件数 2* BO
将 UI 元素和 BO 分开,让 Presenter 成为它们之间的桥梁。
在 BO 本身上实现方法以在 winform 上呈现(假设为单个 UI)。 由于不能直接添加到winform,所以创建一个CustomForm
object 允许添加这样的元素(假设IMyObj
),并调用CustomFOrm.Render()
,最终调用所有子元素的渲染方法。 表单和控件的呈现方式几乎相同。
任何其他方式
在我看来,第2点是更好的方法。 请建议将 BO 与 UI 映射的不同方式的优缺点,以及如何同步它们。 游戏开发者可能有更好的理解。
编辑
我的错误,可能有很多 LED 和开关。 每个开关可以连接到多个 LED。 我创建的类也独立于 UI。 我不指望如何找到控件和发光的解决方案,但是如果给你这些类并告诉你制作一个winform应用程序,那么最好的实现方法是什么,假设你最少会接触这些类并编写最少的代码,以及以下标准的 UI 开发方式
I've always been taught that referencing goes before inheriting, that if you don't have to access protected members of a certain class you better create an object of that type in your class than inherit the whole class (correct me please if im wrong )。
在那种情况下,选项 3 对我来说似乎是最好的。
在 C#(好吧,WinForms)中,每个 UI 元素都有一个Tag
成员,您可以在其中放入任何您想要的内容。 因此,当用户按下“开关”时,代码可以遍历控件以查找具有与当前选择匹配的Tag
的控件。
例如,假设 LED 图像的Tag
字段中有文本“LED=”,那么您可以编写 function 来设置发光 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.