显示基于具体对象类型的动态 UI?

发布于 2024-10-26 04:34:54 字数 1165 浏览 4 评论 0原文

我有一个基类和几个从它派生的具体类。我们可以说它看起来像这样:

abstract class Base
{
    public int id;
    public string baseVariable;
}

class ConcreteA extends Base
{
    public string propertyA;
    public float propertyB;
}

class ConcreteB extends Base
{
    public decimal propertyC;
    public RandomClass propertyD;
}

我们可以说 UI 是一个很大的开放空间,其中漂浮着一堆从 Base 派生的具体对象。在此视图中,您需要的只是常规 idbaseVariable 属性,以获得显示通用对象所需的内容。如果双击其中一个对象,则会弹出一个窗口,您可以在其中编辑基础对象和混凝土对象的所有属性。

在该窗口中,我希望根据我拥有的具体对象类型动态加载选项卡,以及用于修改常见 Base 属性的静态加载选项卡。是否有一个好的设计模式来实现这样的事情?

看来我应该避免这种情况:

class PopUpWindow
{
    public PopUpWindow(object:Base)
    {
        tabs.add(BaseTab);

        switch(typeOf(object))
        {
            case ConcreteA:
                tabs.add(ConcreteATab);
            case ConcreteB:
                 tabs.add(ConcreteBTab);
        }
    }
}

但我也不认为我应该像这样在对象本身中混合 UI 内容:

public PopUpWindow(object:Base)
        {
            tabs.add(BaseTab);
            tabs.addAll(object.TabsToShowOnEdit);
            ...

有什么想法或建议吗?抱歉问了这么长的问题。提前致谢。

I have a base class and several concrete classes that derive from it. Lets just say it looks like this:

abstract class Base
{
    public int id;
    public string baseVariable;
}

class ConcreteA extends Base
{
    public string propertyA;
    public float propertyB;
}

class ConcreteB extends Base
{
    public decimal propertyC;
    public RandomClass propertyD;
}

Lets just say the UI is a big open space with a bunch of concrete objects that derive from Base floating around in it. In this view all you would need is the general id and baseVariable properties to get what you need to display the generic objects. If you double click on one of the objects, it would then pop up a window where you could edit all the properties of the Base and Concrete objects within.

In that window, I want to have dynamically loaded tabs based on which type of concrete object I have, and statically loaded tabs that are used to modify common Base properties. Is there a good design pattern for implementing something like this?

It seems like I should avoid this:

class PopUpWindow
{
    public PopUpWindow(object:Base)
    {
        tabs.add(BaseTab);

        switch(typeOf(object))
        {
            case ConcreteA:
                tabs.add(ConcreteATab);
            case ConcreteB:
                 tabs.add(ConcreteBTab);
        }
    }
}

But I also don't think I should be mixing UI stuff in the objects themselves like so:

public PopUpWindow(object:Base)
        {
            tabs.add(BaseTab);
            tabs.addAll(object.TabsToShowOnEdit);
            ...

Any ideas or suggestions? Sorry for the long winded question. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-11-02 04:34:54

一种方法是拥有一个工厂,您可以在其中根据模型类型注册视图类型:

class ViewFactory
{
  // Can be populated from outside this class
  Dictionary<Type, Type> viewTypeByModelType;

  Control CreateView(Object model)
  {
    return (Control)Activator.CreateInstance(viewTypeByModelType[model.GetType()]);
  }
} 

然后您可以在填充 PopupWindow 时轻松使用该工厂:

 tabs.add(ViewFactory.CreateView(object));

One way is to have a factory in which you register view types based on model type:

class ViewFactory
{
  // Can be populated from outside this class
  Dictionary<Type, Type> viewTypeByModelType;

  Control CreateView(Object model)
  {
    return (Control)Activator.CreateInstance(viewTypeByModelType[model.GetType()]);
  }
} 

Then you can easily use the factory when populating your PopupWindow:

 tabs.add(ViewFactory.CreateView(object));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文