Use UserControl, TemplatedControl, and vbusercontrol correctly

Source: Internet
Author: User

Use UserControl, TemplatedControl, and vbusercontrol correctly

I. First, let's take a look at UserControl.

Anyone familiar with XAML knows that when we create a user control, VS will automatically generate a XXX for us. xaml file and XXX .. xaml. the cs file and the XAML file are used to design the UI interface of the control. The corresponding CS file is used to write the logic code. Let's take a look at the CS file and open the CS file. We will find that the custom control we created inherits from UserControl (do not forget that the corresponding XAML also uses UserControl as the root node ), we can press the F12 key to see UserControl:

We can see that UserControl inherits a "Control" class and an "IUserControl" interface. For now, we only look at the "IUserControl" interface. Continue to F12 and see what it is:

It is very simple. There is only one Content field, but don't take it seriously. It is of the UIElement type! :). If you don't understand what it is, don't worry. Let's take a look at the things in "UserControl:

Now we can understand that the control can be directly written in the Custom User Control thanks to it. This is similar to creating a simple image button.

In the SDK, UserControl is described as follows:Provides a base class for defining a new control that encapsulates related existing controls and provides its own logic.A simple understanding is to define a base class for the new control, but this new control is limited, that is, it is created on the basis of the existing control. This indicates that using UserControl to create a custom control is not very powerful. It can only meet the needs of basic custom controls. If your custom control is only composed of several existing controls, simple style modification, and simple property event registration, it can still meet your needs, however, if you want to modify it more deeply, it is not as simple as you think. For example, if you want to uninstall some events that your custom control does not support but UserControl has already registered for you, this is undoubtedly an addition to the image!

This is like building a house. Microsoft has already built the skeleton of the most popular house for you. The kitchen, bedroom, and living room are all set for you. You just need to follow this. How can you decorate the room? You don't care about it, but you want to move the kitchen to the living room, the position of the living room is moved to the bedroom to build your own villa. He will allow you to build it, but the consequences are at your own risk and the workload is heavy. Well, it's terrible to think about it! If it were your house, would you be willing to do that?

Ii. Next, let's take a look at TemplatedControl.

TemplatedControl is a real custom control, which is more flexible than UserControl. It only provides you with the most basic attributes and methods as a control. So now you are using the "Xiaomi + rifle" equipment for custom controls.

When we create a CustomControl, VS automatically creates a XXX. cs file and a Generic. xaml file located in the Themes folder. The Generic. xaml file is mainly used to write the style of the custom control, but it is different from the normal XAML file, it is written in the style syntax format.

 

Looking at the above code, we can obviously note that this custom control is written in Style mode. TargetType indicates which declared custom control we need to write styles, controlTemplate indicates that we want to design the control template style, and then the element in Border is the style settings you need. The code in this file is basically not a big problem, but it should be noted that since Generic. xaml is a style file, VS cannot display it in real time.

Next, let's take a look at the CustomControl. cs file we created:

The code is very simple, just a constructor, which adds a statement to declare the default style of the control. Let's take a look at its base class: Control. The most obvious difference between it and the user Control in the background code is that it only inherits the Control base class and does not inherit the IUserControl interface, therefore, there is no way to directly place controls in it. Writing like the following code is incorrect:

As for how to solve the problem, it is not the focus of this Article. If you are interested, you can use Baidu. Let's continue. We also found that the XAML designer has considered our custom controls as the smallest unit of control and does not allow them to nest child controls, no matter how complicated your internal implementation is, it seems to me as a whole. Since our custom Control only inherits Control, sometimes we have to define some attributes, methods, and events by ourselves. This is not a bad thing, we can also better customize controls.

The SDK describes Control as follows:IndicatesUIBase class of the element, which can be usedControlTemplateTo define its appearance. UsedContentControl,UserControl,ItemsControlAnd several actual control parent classes.It is easy to understand this sentence. its applicability is far greater than UserControl. This further improves the flexibility of writing custom controls.

This is like building a house. Microsoft has already prepared the basic raw materials required for building a house, but he hasn't built the skeleton of the house for you. You don't have to worry about the raw materials, you need to do everything else. What kind of house is built depends on your ability.

Iii. Suggestions

If there are many controls in a Page, all kinds of controls are available, and the layout is relatively troublesome, how do you deal with the Page layout? Do they all need to be written to a XAML file? What should I do if the code is acceptable but thousands of lines are accidentally written? Therefore, reasonable use of user controls and template controls is an ideal choice. Is my personal layout rules in use, for reference only:

Iii. Summary

As Tony Champion (MVP) said: UserControls are easier to create and maintain. however, custom controls are more flexible. since the fish and the bear's paw cannot have both sides, it depends on your choice. In actual projects, several common controls are often put into one user control for easy maintenance. However, if you want to write a relatively complex control from scratch, the logic inside is much more complex than that of a common control. Do you still choose to use a user control? In my opinion, UserControl is a convenient management control, and TemplatedControl is a real custom control. The TemplatedControl can also do what UserControl can do, but the TemplatedControl does not necessarily do what it can do. In short: When you have multiple simple controls that need to be combined and reused multiple times, we recommend using userControl. When you need to completely write a custom control, we recommend that you use TemplatedControl.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.