ASP.Net 服务器控制复杂的“属性”

发布于 2024-07-17 06:12:42 字数 933 浏览 7 评论 0原文

我要提前道歉,因为我对此的理解确实有限,所以如果我没有很好地解释这一点......抱歉......

无论如何,我正在尝试创建一个asp.net服务器控件,它具有可以使用智能感知设置复杂的属性。 因此,作为示例,我将使用汽车,因此服务器控件可能称为汽车,当我实际在 Web 表单上实现该控件时,我想设置复杂的分层属性,例如:

<Control:Car Color="Paint.Metalic.CandyRed" 
             Wheels="Forged.Aluminun.FiveSpoke.GunMetal" />

或者

<Control:Car Color="Paint.Matte.Yellow"
             Wheels="Forged.Steel.SevenSpoke.BareMetal" />

我尝试在服务器控件中创建公共属性这些只是指向基类的类型/类,但智能感知不会产生任何结果。 我可以使用直接的枚举,它会显示出来,但我不能以这种方式做任何分层的事情。 我一直在寻找例子,但似乎找不到任何东西。 任何帮助将不胜感激!!!!

谢谢你!

另一个不同的例子怎么样,因为项目之间的关系或其预期价值虽然完全不重要,但似乎是一个问题。

让我们考虑大陆/国家/州/城市/等之间的关系...通过这个例子,如果我的自定义服务器控件称为“位置”,那么我希望能够...

<Control:Location CurrentLocation="UnitedStates.Nebraska.Lincoln" />

或者

<Control:Location CurrentLocation="Europe.Italy.Napoli" />

I am going to aplogize in advance because I am really at the limits of my understanding on this so if I do not explain this well....well sorry...

Anyway I am trying to create an asp.net server control that has complex properties which can be set using intellisense. So as an example I will use cars, so the server control might be called car and when I actually implement the control on a webform I want to set complex, hierarchical properties so for example:

<Control:Car Color="Paint.Metalic.CandyRed" 
             Wheels="Forged.Aluminun.FiveSpoke.GunMetal" />

or

<Control:Car Color="Paint.Matte.Yellow"
             Wheels="Forged.Steel.SevenSpoke.BareMetal" />

I have tried creating public properties in the server control that are just types/classes that point to the base class but intellisense doesn't come up with anything. I can use a straight forward enum and that will show up but I can't do anything hierarchical that way. I've been looking for examples but I can't seem to find anything. Any help would be greatly appreciated!!!!

Thank you!

How about a different example as it seems the relationship between items or their intended value, although completely unimportant, seems to be an issue.

Let's take the relationships between continent/ country / state / city / etc.... By this example, if my custom server control is called "Location" then I would like to be able to ...

<Control:Location CurrentLocation="UnitedStates.Nebraska.Lincoln" />

or

<Control:Location CurrentLocation="Europe.Italy.Napoli" />

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

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

发布评论

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

评论(3

酒中人 2024-07-24 06:12:42

你的问题比前两个答案所暗示的更糟糕:你不知道自己在做什么。

抱歉,听起来很刺耳,但是您在哪里见过类似“Paint.Metalic.CandyRed”的东西,它的含义是什么? 或者“Forged.Aluminun.FiveSpoke.GunMetal”? 你到底想表达什么意思?

首先,弄清楚你想代表什么。 然后,创建一个可以代表它的类。 然后,将该类的属性添加到服务器控件。 您可能必须添加 TypeConverter 或其他设计器支持,以便 ASP.NET 将您首选的文本表示形式转换为该类的实例。 否则,您将能够获得诸如字体属性之类的东西。


我将猜测其中一些值代表什么,并尝试向您展示如何在控件中处理它们。 不过,我的猜测可能还很遥远。

我将使用“Paint.Metalic.CandyRed”。 我假设这适用于汽车定制领域,并且 Color 属性旨在代表汽车的整体外观。 在这种情况下,“Paint”可能是一个enum,指的是饰面类型(尽管我不知道还有哪些其他类型的饰面适用于汽车!)。 我小时候制作汽车模型时知道,油漆可能是金属的、光泽的或平面的,所以这三个将是一个枚举的枚举值。 “CandyRed”将是众多颜色中的一种。 这将给出类似这样的结果:

public enum FinishType
{
    Paint,
    NotPaint // _I_ don't know!
}

public enum PaintFinish
{
    Metallic,
    Gloss,
    Flat
}

public enum CarColor
{
    CandyRed,
    SilverMist,
    DesertSandMica,
    MagneticGray,
    // etc.
}


public class CarFinish
{
    public FinishType FinishType {get;set;}
    public PaintFinish PaintFinish {get;set;}
    public CarColor CarColor {get;set;}
}

public class Car : WebControl
{
    public CarFinish Color {get;set;}
}

这将允许类似这样的结果:

<Control:Car Color-FinishType="Paint"
             Color-PaintFinish="Metallic"
             Color-CarColor="CandyRed" .../>

或这样:

<Control:Car ...>
    <Color FinishType="Paint" PaintFinish="Metallic" CarColor="CandyRed"/>
</Control:Car>

Your problem is worse than the previous two answers suggest: you don't know what you're doing.

Sorry to sound so harsh, but where did you ever see something like "Paint.Metalic.CandyRed", and what did it mean there? Or "Forged.Aluminun.FiveSpoke.GunMetal"? What do you even want that to mean?

First, figure out what you want to represent. Then, create a class that can represent it. Then, add a property of that class to the server control. You may have to add a TypeConverter or other designer support in order for ASP.NET to convert your preferred textual representation into an instance of the class. Otherwise, you'll be able to get something like the properties of a Font.


I'm going to make a guess about what some of these values represent, and try to show you how to deal with them in a control. My guess could be far off, though.

I'll work with "Paint.Metalic.CandyRed". I'll assume this applies to the domain of automobile customization, and that the Color property is meant to represent the finish given to the car as a whole. In that case, "Paint" would probably be an enum referring to the type of finish (though I don't know what other sorts of finish apply to a car!). I know from building model cars when I was a kid that paints may be metallic, or gloss, or flat, so those three would be enum values of one enum. "CandyRed" would be one of many colors. This would give something like this:

public enum FinishType
{
    Paint,
    NotPaint // _I_ don't know!
}

public enum PaintFinish
{
    Metallic,
    Gloss,
    Flat
}

public enum CarColor
{
    CandyRed,
    SilverMist,
    DesertSandMica,
    MagneticGray,
    // etc.
}


public class CarFinish
{
    public FinishType FinishType {get;set;}
    public PaintFinish PaintFinish {get;set;}
    public CarColor CarColor {get;set;}
}

public class Car : WebControl
{
    public CarFinish Color {get;set;}
}

This would allow for something like this:

<Control:Car Color-FinishType="Paint"
             Color-PaintFinish="Metallic"
             Color-CarColor="CandyRed" .../>

or this:

<Control:Car ...>
    <Color FinishType="Paint" PaintFinish="Metallic" CarColor="CandyRed"/>
</Control:Car>
握住我的手 2024-07-24 06:12:42

这些项目需要是枚举,因为这是至少在我所看到的所有内容中支持它的唯一方式。 现在,您可以通过一些枚举来完成您想要的事情。

namespace Paint
{
    public enum Metalic
    {
        CandyRed
    }

    public enum Matte
    {
        Yellow
    }
}

当然,并不完美,但易于记录和理解!

These items would need to be enums as that is the only way that it is supported at least in everything I have seen. now, you can accomplish what you want with a few enums.

namespace Paint
{
    public enum Metalic
    {
        CandyRed
    }

    public enum Matte
    {
        Yellow
    }
}

Granted, not perfect, but easy to document and understand!

橘虞初梦 2024-07-24 06:12:42

正如 Mitchel Sellers 所发布的,它们需要是枚举或常量/静态。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Paint
{
    public class Metallic
    {
        public static Color CandyRed
        {
            get { return Color.Red; }
        }

        public static Color CandyGreen
        {
            get { return Color.Green; }
        }
    }

    public class Matte
    {
        public static Color Red
        {
            get { return Color.Red; }
        }

        public static Color Green
        {
            get { return Color.Green; }
        }
    }
}

As Mitchel Sellers posted, they will need to be either Enum or Constants/Statics.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Paint
{
    public class Metallic
    {
        public static Color CandyRed
        {
            get { return Color.Red; }
        }

        public static Color CandyGreen
        {
            get { return Color.Green; }
        }
    }

    public class Matte
    {
        public static Color Red
        {
            get { return Color.Red; }
        }

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