There are many articles about "delegation", "Events", and "interfaces" in the garden. There are many good articles. In this article, we will introduce a development instance of "Windows Phone", the. NET mechanism, and its own crude understanding.
First, let's look at all the instance code:
Background code:
Public class notepaditem: inotifypropertychanged
{
Public String _ title;
Public String title
{
Get {return _ title ;}
Set
{
_ Title = value;
Notifypropertychanged ("title ");
}
}
Public String _ intro;
Public String intro
{
Get {return _ intro ;}
Set
{
_ Intro = value;
Notifypropertychanged ("Intro ");
}
}
Public String _ cont;
Public String cont
{
Get {return _ cont ;}
Set
{
_ Cont = value;
Notifypropertychanged ("Cont ");
}
}
Public event propertychangedeventhandler propertychanged;
Private void policypropertychanged (string propertyname)
{
If (null! = Propertychanged)
{
Propertychanged (this, new propertychangedeventargs (propertyname ));
}
}
}
Front-end XAML code:
<Controls: drawing title = "Mobile Phone safe deposit box">
<! -- Other item one -->
<Controls: javastitem header = "secret Notepad">
<! -- Double line list with text wrapping -->
<ListBox X: Name = "firstlistbox" margin = "0, 0,-12, 0" itemssource = "{binding items}" selectionchanged = "firstlistbox_selectionchanged">
<ListBox. itemtemplate>
<Datatemplate>
<Stackpanel margin = "432," width = "">
<Textblock text = "{binding title}" textwrapping = "Wrap" style = "{staticresource phonetextextralargestyle}"/>
<Textblock text = "{binding intro}" textwrapping = "Wrap" margin = "12,-6, 12, 0" style = "{staticresource phonetextsubtlestyle}"/>
</Stackpanel>
</Datatemplate>
</ListBox. itemtemplate>
</ListBox>
</Controls: effectitem>
Start analysis, let go...
Function implemented by this Code: bind the information to the ListBox control. When the data source information changes, the information displayed on the page also changes. (Simple functions: mainly introduces "delegation", "Events", and "interfaces ")
1. "inotifypropertychanged" Interface)
// Summary:
// Notifies clients that a property value has changed.
Public interface inotifypropertychanged
{
// Summary:
// Occurs when a property value changes.
Event propertychangedeventhandler propertychanged;
}
"Interface" is a set of standards, and then the specific methods are implemented by "implementation class", which can be understood as a set of abstract classes. If any class implements this interface, that is, implementing the "propertychanged" method, it can "do something ". "These things" refer to events triggered by changes to class attributes.
Note: "Any Class" here inherits it, and they all have the same functions to implement, so that everyone remains consistent. Instead of everyone declaring their own, this ensures standardization. To change everyone together, you can also understand the advantages of "interfaces" as "module interconnection standards ".
Ii. Delegation)
You can see "propertychangedeventhandler" in the interface, and it is a delegate to see its definition.
Public Delegate void propertychangedeventhandler (Object sender, propertychangedeventargs E );
If you want to call another method in one method, but they are not under a "class" or even under the same namespace. Or you want to call a batch of methods in a method. These methods have similar methods, but some attribute variables are different. This is a stupid way to use conditions or enumeration for judgment. So what if a new method is added ??? This involves the scalability of the project. What should I do? Directly pass these methods as parameters into the target method, that is, "using methods as parameters ".
Since the method is a parameter, there should be a "parameter type" for specifying the type of the parameter represented by the method. It is like a parameter of the "string" type. This "method" type parameter is defined as "Delegate ". The delegate is actually a class. Although the definition method is similar to the definition method, can the type be defined. It becomes a class during compilation. It is precisely because it is a class that can be declared in all declared classes, that is, the delegate can be directly declared in the namespace.
In this way, you can directly pass the method as a parameter into another method. In the method, you only need to call the "parameter" to call the passed "method ". You don't have to worry about the judgment of the execution method. You can execute whatever you want.
Since "delegate" is a type, you can use it to define variables.
For example, there are two classes, one "manager" and the other "employ". What should I do if I call the method in "employ" in "manager? In this case, you can declare a delegate "managerdelegate", use this delegate to define a variable "delegate", and assign the method in "employ" to "deleegate ", then, pass "delegate" into the "manager" method. Is it the same as using a type variable, except that a variable in the delegate can not only assign a value to "One method", but also "bind" more methods. The first value must be "=", and "+ =" will be bound later to remove "-= ".
Imagine what if we put the "delegate" variable into the "manager" class?
Add if (delegate! = NULL) {delegate () // call the delegate method} else {} and directly execute "delegate-defined variable: Delegate, so there is another name for this "deltegate". Do you know that? By mistake, it is an "Event ".
3. Events)
Before the variable "delegate" is changed to "Event", a key sub-item "Event" must be added to enhance encapsulation.
In a word, an event is a variable of the encapsulated delegate type.
This is the origin of "Event" in "event propertychangedeventhandler propertychanged.
In short: the interface is the "module docking" standard, and we need to change it together. The delegate is to take the "method" as the parameter and the corresponding parameter type. An event is a variable defined by the delegate type and encapsulated by "Event.
This is just a humble understanding and opinion. There must be a lot of improper things. Please give me more advice.
The overall implementation process and. Net implementation mechanism have not yet been introduced. These contents will be described below, so stay tuned.
Expand resources