且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在面板顶部添加控件

更新时间:2023-12-06 10:41:28

对于这类问题,我有一个非常通用的食谱.

您怀疑如何在代码中创建UI的某些部分?但是,您可以仅使用设计器制作想要的UI设计示例吗?如果是这样,请在某些研究项目的设计师中进行.进行裸露布局;您以后可以在代码中添加的所有其他内容.可能只需要几分钟.编译并运行该示例以确保它可以按照您的要求运行.

完成此操作后,找到在设计时自动生成的代码.如果您不知道在哪里找到它,请按子字符串使用文件搜索(对于WPF,请不要从Visual Studio进行搜索,请从外部搜索),对于搜索子字符串,请使用由以下成员创建的成员的某些成员名称:设计的(对于WPF,至少创建一些UI元素的Name属性).

现在,看看自动生成的代码并了解其工作原理.这真的很简单.



您应该了解每个控件都是动态添加的.设计的代码仅有助于生成代码,但是一旦生成,它就可以在运行时以与手工编程完全相同的方式运行.请记住,在很多情况下,手动编程会更快(因为使用鼠标"进行编程是一项人工操作,因此无法使用高效的编程抽象,例如循环和代码重用),并且可以提供更好的质量,因此您的要求非常合理.

使用设计器进行学习的方法已证明是有用的.

—SA
I have a very general cookbook recipe for many problems of such kind.

You have a doubt how to create some part of UI in code? But can you make just a sample of the UI design you want using the designer? If so, do it in designer in some research project. Do bare layout; everything else you can add later in code. It may take just few minutes. Compile and run the sample to make sure it works as you wanted.

When this is done, locate the code auto-generated during design time. If you don''t know yes where to find it, use file search by sub-string (for WPF, do it not from Visual Studio, search from outside), for a search substring, use some member name of the member created by the designed (for WPF, create at least one Name attribute of some UI element).

Now, take a look at the auto-generated code and learn how it works. This is really simple.



You should understand that every control is added dynamically. The designed simply helps to generate the code, but once generated, it works during run time exactly the same way you would program it by hand. Remember that in many cases programming by hand is faster (because programming "with mouse" is a manual labor, it cannot use efficient programming abstractions like loops and code reuse) and can give better quality, so your request makes perfect sense.

The method of using designer for learning has proven its usefulness.

—SA