在.net中创建用户控件时需要考虑哪些问题? [英] what concerns we need to consider while creating a user control in .net?

查看:38
本文介绍了在.net中创建用户控件时需要考虑哪些问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我创建一个用户控件.
通过不同的源调用时,此控件需要表现不同.
我该如何处理呢?

Say i create a user control.
this control needs to behave diffrently when invoked through different sources.
how do i handle that?

推荐答案

Roopica写道:当通过不同的源调用时,此控件需要表现不同."

1.在这里需要确切地了解"调用"的含义:您的意思是:在设计时添加到窗体或其他容器控件 ,或由表单,并在运行时处添加?

请记住,在设计时或运行时,您可以将UserControl添加到:窗体,或窗体上的其他一些容器控件(如面板)上,或:到一系列复杂的嵌套容器控件,例如:插入到窗体上的面板中的面板中!

2.您需要准确描述"具有不同的行为"的含义:更改某些UserControl的属性,变量值或控件的EventHandler ...或?

RyanB对您的问题的评论:建议UserControl具有各种不同的可能的设施(方法,事件处理程序,变量,属性),不同的宿主容器可以利用这些设施:我认为这不是最佳选择;我认为这违反了关注点分离"的原则.它在UserControl和特定的主机"之间创建特定的依赖关系.
恕我直言,更为可取的是让UserControl通过公共属性公开"公开其内部对象,然后可能的是,不同的主机"可以修改其状态,属性或事件处理程序.

例如:我的UserControl中有一个名为"FirstButton"的按钮:在此UserControl的"Load EventHandler"中,我有一些类似这样的代码:
Roopica wrote: "this control needs to behave diffrently when invoked through different sources."

1. need to know exactly what you mean by "invoked" here: do you mean: added to a Form, or other Container Control at design-time, or created by a Form, and added, at run-time ?

Remember that at design-time, or, at run-time, you could be adding the UserControl to: either a Form, or to some other Container Control, like a Panel, on a Form, or: to some oomplexly nested series of Container Controls, for example: into a Panel within a Panel on a Form !

2. you need to describe exactly what "behave differently" means: to change certain of the UserControl''s properties, variable value, or EventHandlers for Controls ... or, ?

RyanB''s comment on your question: proposing the UserControl have a variety of different possible facilities (methods ? event-handlers ? variables ? properties ?) that different host containers can make use of: I believe is not optimal; I believe it violates the principle of "separation of concerns." It creates specific dependencies between the UserControl, and specific "hosts."

Far more desirable, imho, is to have the UserControl "publicly" expose its internal objects through public properties, which it''s possible different "hosts" can then modify state, properties, or event-handlers of.

For example: I have a button named ''FirstButton'' in my UserControl: in the ''Load EventHandler of this UserControl, I have some code like this:
public Button FirstButton { get; set; }

private void UserControl1_Load(object sender, EventArgs e)
{
    FirstButton = this.button1;
}

现在,每次创建UserControl1的实例时,主机可以通过其公共属性"FirstButton"访问按钮并更改其属性:但是我们必须考虑两种情况:

1.在设计时,hss用户已经通过拖放将UserControl放置在Form上:在这种情况下,我们可以像这样使用Form Load EventHandler:

Now every time an instance of UserControl1 is created, the "host" can access the button, through its public Property, "FirstButton," and change it''s properties: but we have to consider two cases:

1. the user at design time hss already placed the UserControl on the Form by drag-drop: in that case we can use the Form Load EventHandler like this:

// declare a variable of Type UserControl
FirstUserControl frmUC;
//
private void Form1_Load(object sender, EventArgs e)
{
    // does the UserControl exist on this Form now ?
    // note the requirement to use a string here
    frmUC = this.Controls["firstUserControl1"];

    if(frmUC != null)
    {
        // UserControl present so we convert it
        // to the specific type of our UserControl
        FirstUserControl firstUC = frmUC as FirstUserControl;

        // get the button
        Button theButton = firstUC.FirstButton

        // assign an event handler to it
        // EventHandler code not shown here
        theButton .Click += FirstButton_Click;

        // set a visual property of the UserControl
        firstUC.BackColor = Color.WhiteSmoke;

        // set visual properties of the Button
        theButton.BackColor = Color.MintCream;
        theButtonClick += click1;
    }
}

2.用户在运行时添加了UserControl的实例:在这里,我们可以使用"ControlAdded EventHandler"形式作为我们的通知机制:

2. The user is adding an instance of the UserControl at run-time: here we can use the Form ''ControlAdded EventHandler as our notification mechanism:

private void Form1_ControlAdded(object sender, ControlEventArgs e)
 {
     Console.WriteLine("control added at run-time");

     // have we just added an instance of our specific UserControl ?
     if(e.Control is FirstUserControl)
     {
         FirstUserControl RunTimeAddedFirstUserControl = e.Control as FirstUserControl;

        // and now add can an event handler to it
         RunTimeAddedFirstUserControl.FirstButton.Click += RunTimeAddedFirstButton_Click;
     }
 }

摘要:对于简单的场景,有多种方法可以轻松地修改UserControl的视觉属性,状态或行为:已经存在于容器中(在设计时拖放),或者在运行时添加到容器(动态).

这样的优点是,可以使UserControl定义保持简单明了",并且不会在UserControl中的特定窗体(主机")和特定功能之间创建依赖关系.

对于更复杂的场景,在设计时涉及具有多个UserControl实例的容器控件,或者在运行时创建具有相同UserControl的实例的多个容器:这可能不是最佳做法,并且行为复杂,并且与同一UserControl的每个不同实例相关联的视觉外观:此策略可能需要太多的复杂性.

在上述更为复杂的情况下,最好是分析所有不同的UserControl需求,并尽可能定义一个基本的UserControl类,其他UaerControls将从中继承并修改/覆盖它. br/>
最好,比尔

Summary: for simple scenarios there are ways to easily modify the visual attributes, state, or behavior of UserControls either: existing already in a container (drag-dropped at design-time), or, added at run-time to a container (dynamic).

This has the advantage of leaving the UserControl definition "simple and dumb," and not creating dependencies between specific Forms ("hosts") and specific facilities in the UserControl.

For more complex scenarios, involving Container Controls with more than one instance of the same UserControl at design-time, or having multiple instances of the same UserControl create at run-time: this may not be a best practice, and with complexly different behaviors and visual look associated with each different instance of the same UserControl: this strategy may require far too much complexity.

In the more complex scenario described above, it is probably best to analyze all the different UserControl requirements, and, if possible define a base UserControl class from which the differently behaving, and looking, other UaerControls inherit from and modify/over-ride.

best, Bill


这篇关于在.net中创建用户控件时需要考虑哪些问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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