Silverlight-Caliburn application framework 1
Silverlight-Caliburn application framework 2
Silverlight-Caliburn application framework 3
Silverlight-Caliburn application framework 4
Silverlight-Caliburn application framework 5
Silverlight-Caliburn application framework 6
The previous article mainly introduced how to configure the Caliburn framework in Silverlight. Now we can understand the use of the Caliburn Action feature on this basis. Previously, we mentioned the Action feature to enable Silverlight binding to support methods, from this perspective, the UI can be bound to a method.
The example here is very simple, that is, finding the Square Value of a number, so that our attention is more focused on the framework itself.
First, configure Caliburn according to the previous steps. Now let's take a look at the architecture.
Now we first define the class extraction:
Public ClassExtraction
{
Public DoubleExtrace (DoubleParam)
{
ReturnMath. SQRT (PARAM );
}
}
Extrace () is defined in the class to return the Square Value of a number,
The UI is designed as follows:
<TextboxName="Txtinput"/>
<TextboxName="Txtresult"/>
datainput : label name = " label1 " content = " input: " />
<Datainput:LabelName="Label2"Content="Result :" />
<Button Content="Computing> </Button>
Pay attention to our name here, because the names of the two text boxes will be used later, and the final effect is like this:
Here we use a textbox to enter a number, and then display the result in the result text box. Now we can operate on XAML and add relevant references first:
Xmlns: Local = "CLR-namespace: Actions"
Xmlns: CA = "CLR-namespace: Caliburn. presentationframework. Actions; Assembly = Caliburn. presentationframework"
Xmlns: pF = "CLR-namespace: Caliburn. presentationframework; Assembly = Caliburn. presentationframework"
Xmlns: Ct = "CLR-namespace: Caliburn. presentationframework. triggers; Assembly = Caliburn. presentationframework"
We have introduced several related classes above. Only one of them can be referenced in WPF. This type of support is missing in SL, so each class needs to be referenced separately, these classes will be used later,
Now we need to specify the target object of the action in the root element.
<CA:Action. Target><Local:Extraction /></CA:Action. Target>
Now we are concerned about how to associate the button with extrace (). Here is the role played by Caliburn. We will change the button:
button content = " computing " height = " 23 " horizontalalignment = " Left " margin = " 71,120, " name = " button1 " verticalignment = " TOP " width = " 75 " >
<PF:Message. triggers>
<PF:Routedmessagetriggercollection>
<CT:Eventmessagetrigger Eventname="Click">
<CT:Eventmessagetrigger. Message>
Ca : actionmessage methodname = " extrace " outcomepath = " txtresult. text " >
<PF:Parameter Elementname="Txtinput" Path="Text" />
</CA:Actionmessage>
</CT:Eventmessagetrigger. Message>
</CT:Eventmessagetrigger>
</PF:Routedmessagetriggercollection>
</PF:Message. triggers>
</Button>
The aboveCodeWe mainly use the message. triggers additional attribute. With this attribute, we can add a series of trigger information to any element. Here we will take a look at eventmessagetrigger,
It can trigger an event to send a message to the Controller. In the above Code, when we click the button, an actionmessage indicating that extrace () will be accessed will be sent. Before sl3, because it does not support directly binding to parameters, therefore, this effect is achieved through the elementname and path attributes. Here, the text attribute value of the element txtinput is passed as a parameter to extrace (), and the return value of the method is bound to txtresult. text property.
Who will operate the trigger? Have you seen the action. Target declared in the root element? An extraction class is instantiated in it to process actionmessage.
Now ourProgramIn fact, the function of finding the square value has been implemented, but it seems that there are many XAML labels in the button above. It doesn't matter. Caliburn provides us with a series of simplified methods, let's take a look at it one by one:
<Button Content="Computing" PF:Message.Attach="[Event click] = [Action extrace (txtinput. Text): txtresult. Text]">
This is a good way to declare a trigger or message. Message is used. attach, which can parse a string as a trigger. On the left of the equal sign, we declare the trigger type and parameters. Here, eventmessagetrigger is declared for the click event, the right side of the equal sign declares the type and content of the message. The explanation here is the same as that in the first method. That is, a parameter is declared from txtinput. text, and the return value is bound to txtresult. the actionmessage of text. The "[]" here can be omitted.
If necessary, you can also specify the binding mode for the parameter:
<Button Content="Computing" PF:Message.Attach="Event click = action extrace (txtinput. Text: twoway): txtresult. Text" />
Now let's look at the second method:
<Button Content="Computing" PF:Message.Attach="Extrace (txtinput. Text): txtresult. Text" />
Compared with the first method, the trigger type is omitted. Caliburn selects a default Trigger based on the type of the attached object. The default trigger here is eventmessagetrigger and the message type.
Is actionmessage. If the method does not return values or parameters, we can directly write them
<Button Content="Computing" PF:Message.Attach="Extrace"/>
If multiple events are separated by commas (,), as shown in figure
<Button PF:Message.Attach="[Event click] = [Action Method1]; [event Loaded] = [Action method2]" />
For the abbreviated form of action, these conditions are generally used. You can refer to this article.ArticleIn summary, we actually use extrace (element. Property)
In this way, Caliburn actually supports multiple parameter passing methods. For example, you can directly pass a value to it: extrace ('9'); or you can pass a string: extrace ('abcde ');
Bound to the default property of the control: extrace (textbox1); other methods are not listed one by one. For information triggers, Caliburn provides three types: eventmessagetrigger,
Gesturemessagetrigger, attachedeventmessagetrigger. They are in the Caliburn. presentationframework. triggers namespace and can also be used by themselves.
Extended the trigger type from the basemessagetrigger class. eventmessagetrigger is used above. You can view the other two methods and the parameter transmission methods in this document.
Code download: actionincaliburn environment: vs2010 + sl3