I was going to write more details, but recently I want to change my job. So I am very busy, and I feel that there is no need to write so many imaginary things. So I will not follow the outline here, it starts with the code to see the interaction mechanism of Silverlight. the basic classes of dependency attributes and additional attributes are the same, but the purpose of dependency attributes is different from that of additional attributes. Dependency attributes are more attributes, additional attributes are more extensions, which are similar to class extension methods. Additional attributes are very important. Many interactions are actually implemented using this feature, additional attributes provide an entry point for interactive injection of existing UI elements. In a sense, this is also a model of AOP programming. For details about the simulation of dependency attributes, refer to my previous blog post. We will not discuss the dependency attributes here. I will have specific code for the implementation of additional attributes, because these codes are also the key to implementing our own interactive actions.
To implement the Action interaction, we are ready to implement the following functions:
1) can be appended to any interface element;
2) ability to respond to some events of UI elements;
3) can call the set target method (similar to callmethodbehavior). Of course, the parameters here are static settings. If you want to bind them to each other, you can actually use the binding class. You can also parse the path values by yourself.
To improve scalability, we define a myaction base class, a myaction collection myactioncollection. This class is used for a UI element that can have multiple actions at the same time.
A) myaction and myaction <t> classes.
/// <Summary>
/// Provide a public excuse
/// </Summary>
Public abstract class myaction
{
Public myaction ()
{
}
/// <Summary>
/// Provides additional operation methods, which can be called when an element is appended. The injection function can be provided when a specific action is appended.
/// </Summary>
Public abstract void onattached ();
/// <Summary>
/// Opposite to the above functions.
/// </Summary>
Public abstract void ondetaching ();
/// <Summary>
/// Attaches an object, similar to behavior's associatedobject.
/// </Summary>
Public object OBJ {Get; set ;}
/// <Summary>
/// It is useless.
/// </Summary>
Public String myname {Get; set ;}
}
/// <Summary>
/// Define a template type for the convenience of the following inheritance operations. Of course, reflection can also be used here for some processing, but here is a demonstration and nothing special is done.
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
Public class myaction <t>: myaction where T: uielement
{
Public myaction ()
{
}
Public t wobj {get {return (t) (base. OBJ) ;}// replace it with the target generic class, which facilitates subclass writing and does not need to be converted every time.
Public override void onattached ()
{
}
Public override void ondetaching ()
{
}
}
B) Action collection class
/// <Summary>
/// Action collection class, which mainly provides additional functions when the set changes. This class is responsible for passing the attached object to action.
/// </Summary>
Public class myactioncollection: system. Collections. objectmodel. collection <myaction>
{
Public myactioncollection ()
{
}
// Note that action is inserted here. The onattached method is called here to inject the function into the action subclass.
Protected override void insertitem (INT index, myaction item)
{
Item. OBJ = OBJ; // assign the associated object to action to facilitate injection.
Base. insertitem (index, item );
Item. onattached (); // give action a chance to inject a function. Action initialization cannot inject a function, because its associated object does not exist, and there are limited tasks to do.
}
// Same as above. But it is useless.
Protected override void setitem (INT index, myaction item)
{
Item. OBJ = OBJ;
Base. setitem (index, item );
Item. onattached ();
}
// The action is removed to provide an opportunity for the action to release the resource.
Protected override void removeitem (INT index)
{
Myaction theitem = This [Index];
Theitem. ondetaching ();
Base. removeitem (INDEX );
}
// Additional object, of course, is generally defined as dependencyobject.
Public object OBJ {Get; set ;}
Public String myname {Get; set ;}
}
The basic action and its set definition are complete. The following describes how to append this set to page elements.