My understanding of WPF knowledge points and carding notes

Source: Internet
Author: User

The main content of this article is their own use of WPF development, my knowledge of the carding, only for the personal summary, the understanding of a lot of things are still in the exploratory phase, there will always be a fallacy and better development methods, if you can put forward valuable suggestions, grateful.

Although I am not only focused on WPF, but also like all kinds of programming languages, it is estimated that WPF development will continue to be used frequently in the next 1 or 2 years.

Want to go, a long time, a lot of things are sorted and recorded as well, then decided from the release of this article, began to accumulate slowly, with the expansion of personal knowledge and understanding, this article may continue to add or update content, but also as far as possible to retain the same concept before and after different understanding.

WPF has been engaged for almost a year, with the exception of a large WPF project (already commercialized) that led to the development of the company last year, and the rest is a little bit of software. In my opinion

The advantages of WPF are:

1. It is relatively easy to write a fully customized interface.

2. The unique MVVM design pattern can perfectly separate the UI design (view layer) and the business logic (model layer).

The disadvantages of WPF are:

1. The cost of learning is relatively high (I mean really proficient, you and I say drag-and-drop control to generate an event write a method, when a processing tool, it does not have to learn)

The new concept is very much, easy to confuse, I am still very good patience, but still often a lot of days constantly tossing, toss to start spray ms why this design.

2. Cannot cross platform. After a while to study the next mono.

3. Because of its complexity, bugs are sometimes hidden deeper.

4. Many pits, complex projects under a lot of pits must be self-jumping, official documents Most of the time does not solve the actual problem, the online search information is uneven, it is difficult to find what they want, in order to solve the big pits at the beginning often need to understand a lot of the concept of falling from the sky. Foreign download of the demo is sometimes too complicated, many of the source code on the CSDN is too unprofessional ... I just spit out the trough.

So far, I still haven't solved the problem:

A. The image takes up too much memory, and when the picture is many and the real-time refresh is fast, the memory will skyrocket to a crash if you do not write code to manually release the resource. The same functionality, instead of using the WinForm implementation, consumes only a small amount of memory.

B. The Data Virtualization panel appears blank on XP or some Win7 computers. can only be replaced with a normal panel.

C. When the form is set to allow transparency, theWebBrowser appears blank. (The solution currently found and tried is not a perfect solution) my approach is to switch to a third-party browser kernel, such as CEF, but using JavaScript inside and outside the communication is a very troublesome thing. Or just give up the form transparency ...

Recently started using WPF to write a program, but also want to take advantage of this development, the previous WPF related knowledge to do a integration, re-understand the concept, discard bad, explore more comfortable and easy to use, and will know the mastery, as far as possible in the most "elegant" way to achieve the function.

So let's get started.

WPF related technologies must be clear of the knowledge points, first outline, according to my personal recommendations of the order of learning.

1. Understand the principles of XAML -related forms design.

A. The logical tree structure is much more HTML-like, but more cumbersome.

B. You can use XamlPad to view the visual tree structure.

C. Understanding style is similar to CSS and can be changed by changing the resource dictionary at any time to achieve the purpose of changing the theme or skin.

2. triggers (Trigger), most commonly used are property triggers and data triggers.

A. You need to know that triggers are primarily intended for visual interaction.

B. A property trigger is a change in the value of a property in the control itself, such as ismouseover=true, which triggers visual content such as the background color to change.

C. A data trigger is a data template (Datatemplete) that triggers a change in the visual content when a change in a business data change takes place.

3. In order to create different forms of the interface, to achieve a variety of magical effects, you need to learn WPF drawing .

A. Using graphics, including: lines, rectangles, ellipses, Bezier curves , path(most powerful path)

B. Apply filter effect,Effect is relatively simple, but import and development of external filters, has not been studied.

C. Use variants . There are basic deformations such as panning, rotating, zooming, twisting, and matrix deformation. (Note that each variant can be placed either in the rendering deformation or in the layout deformation , and the difference between the two needs to be distinguished.) The deformation is only seen and the actual position and shape are not changed. Layout distortion is a real change that will re-layout the rest of the controls while morphing. )

4. Learn to create simple animations using XAML

A. Attempting to trigger an animation using Class 3 triggers

B. Using Visualstatusmanager to apply animations

C. If not necessary, try to avoid creating animations by writing code

d. (Extension: Use blend to create and assemble complex animations.) )

5. dependency Properties and attached properties .

A. Learn how to customize our own extended dependency properties and attached properties.

B. The so-called dependency property, functionally speaking, is an ordinary property that comes with functionality that can be bound to other properties of any object. The so-called binding is: one value changes, the other value changes. This saves a lot of background code related to interface effects and makes it possible to categorize the interface and business code.

C. Dependency properties can be used to inherit an existing control or custom control and extend the properties for it. The disadvantage is that these properties are not reusable and are proprietary to the control.

D. An attached property is a special dependency property. There are two usages, one that inherits the existing class and is defined to achieve the effect as canvas.left. A new class that is primarily used to extend additional properties to existing controls without extensive inheritance. is a very good combination of pattern thinking.

6. Templates. Main knowledge:controltemplete,datatemplete

A. Controltemplete is used to rewrite the visual structure of an existing control , with a combination of general and dependency properties and attached properties, plus a binding that controls can be well extended.

B. Datatemplete is primarily used to define the visual structure of a data object . Since it is a data object, it is best to have a data type, which is defined in datatype.

C. Templates play a huge role in WPF. Control templates can easily write out the appearance of arbitrary effects of any shape, and data templates make the view layer and the ViewModel layer very well separated. Please be aware that at first I was dismissive of this concept, but I just didn't understand what it meant. Later I understood: because of the existence of data templates, there is almost no more control objects in the code.

D. Recommendation mode in the use of templates, using automatically created templates, often see part_xxx, do not understand how? Looking at the properties of the base class, there will be Templatepart, which is actually a recommended design pattern for prompting later developers to tell you the necessary elements of the control: Name and type. That is, you can override the control template, but if you want to implement the core functionality of the control itself, be sure to keep a control of a type named Part_xxx.

7. MVVM design mode, the most convenient to learn this mode is the mvvmlight framework, which can be downloaded directly from NuGet .

A. Model-view-viewmodel. Unlike design patterns such as MVC,MVP, MVVM's main feature is the separation of the UI (View) and the Business (Model). And ViewModel is responsible for the performance logic. This is especially useful when developing, and you can quickly create design viewmodel so that the design phase can simulate a completely real-life effect, because the ViewModel of the view layer can be easily switched.

B. I am not a transition admirer of design patterns, but it is a good approach as long as the productivity of the code continues to remain relatively high. Many times as long as it does not affect the overall situation, writing some code in the codebehind is understandable.

C. Mvvmlight framework, there is a very good eventtocommand, can be any event directly into the command, in my opinion this makes the UI and business separation more thoroughly.

D. mvvmlight under the message mechanism,Messenger is also a very useful thing, can achieve this unrelated viewmodel communication between, let them continue to have no connection. It can also be used for navigation.

8. Behavior that can be attached to a control (Behavior)

A. Behavior, as well as the above-mentioned trigger, Eventtocommand, are an additional property . That would be a good understanding. The attached property, which is generally understood, is only a value of one by one data types, and since it is extended into a data type with such complex behavior, it is intended primarily to enable the reuse of various behavioral effects, but the encapsulation of behaviors is the most complete .

9. To have a deep understanding of the adorner-related Adorner, Decorator, Adornerdecorator

A. From a simple beginning, it is best to understand the Decorator, if the word is unfamiliar, then border is not unfamiliar with the border, a thing, a side of the outside.

However, decorator is the base class of border. Can extend it, however I have not yet encountered the application scenario of inheriting decorator.

B. Besides adorner, this is the real decorator, itself no visual structure, its existence is to you inherit it, and design its visual style, how to inherit, online a lot of tutorials, the most intuitive understanding is: on the control itself, covered with an extra layer of decoration , for the interactive tips and Actions . For example: cursor, rotate button, resize, table header sort arrows and so on. It's important to draw what you need, nn.

C. Understand the previous two, and then the last one, looks good complex tall on the look, in fact, very simple, according to my understanding: This is a double-sided adhesive , used to paste Decorator and Adorner. Microsoft needs to have a container on the control to put Adorner, the container is best to start from the edge of the control, the edge of the control, naturally decorator, OK, then wrap a thing, you can put Adorner, so the name of this thing is called Adornerdecorator, there is a thing called adornerlayer can let adorner directly add.

D. In fact, only adornerdecorator and scrollcontentpresenter have adornerlayer, so when they want to add their own adorners, Don't forget to set a adornerdeocrator in the template. Especially like me this often writes the custom form, the form's ContentPresenter outside sets a adornerdecorator is must not forget.

routed events and commands

A. If the WPF program you want to write out does not have any puzzling problems in a complex interface, be sure to understand the concepts of routed events and routed commands.

The so-called route, there are three ways of rotten streets: tunnels , bubbling , direct . As the name implies: tunnels, labels respond to events from within. Bubbles, labels the corresponding events from inside out.

Directly, only from defining the element corresponding to the event, writing elsewhere is useless.

B. Routed events are defined at which layer to listen for events , rather than click on which layer will execute the event, the purpose is naturally "Wan Fumo Fumo opens", such as the bubbling route, N more control can respond to a mouse event, then only need to listen to the collection of the event on the control ( Attached events ). In response to event handling, it is possible to get the actual hit control. Of course, this time I tend to directly take the hit control DataContext, directly into the ViewModel, manipulate the data layer.

C. This routed event mechanism is very similar to the event mechanism of JavaScript in HTML, and the third parameter of AddEventListener usecapture is used to control whether it is bubbling (bubbling) mode. I also often use the delegate in JQuery to do similar things in order to listen to events related to future child elements.

D. You can mark Handler = True in a routed event. To prevent the event from continuing routing.  However, even with this setting, the routed event continues to pass. Because the routed event handler is explicitly hooked up with AddHandler, you can define a routed event to be executed even if it is handled. That is, set handledEventsToo = True. Here analysis, believe Handler | | Event handlers are executed when the HandledEventsToo is true.

E. Custom routed events, similar to registering dependency properties. Where you can define the route type and so on.

F. What is to be emphasized is that the event at the beginning of the Preview name is the tunnel mode, which means that the event is delivered from within. Events without preview are bubbling and are passed from inside out. Event preview of the same name executes first. This is an image of the rebound, the ball in the bounce back, the process of the ball can be caught.

G. Routing commands it seems to me that there are fewer applications due to their innate flaws and limitations. I generally use Mvvmlight's relaycommand, of course, if the project is not necessary to introduce mvvmlight, their own custom can also.

Speaking for a long period of the theory, is this less than a year of WPF development experience, and then began to practice the Content section. After the realization of their own feel good things, will be listed below to share.

Combine ItemsControl to dynamically add controls to the most MVVM mode in a canvas

My understanding of WPF knowledge points and carding notes

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.