WPF Caliburn Study Notes (1) Action

Source: Internet
Author: User

Preface:

These contents are based on Caliburn 1.1. Please correct me if you do not understand or make any mistakes.

Basic

When configuring Caliburn, we need to add three references:

    1. Caliburn. Core. dll
    2. Caliburn. presentationframework. dll
    3. Microsoft. Practices. servicelocation. dll

Caliburn not only applies these two references, but also proposes them when necessary.

To use Caliburn to write events, you must configure it in APP. XAML. CS.

Open app. XAML. CS

 
 PublicAPP () {caliburnframework. configurecore (). withpresentationframework (). Start ();}

Don't forget the two references below. The following classes are all used.

Using Caliburn. core;

Using Caliburn. presentationframework;

The app constructor writes the configuration of the Caliburn container. To use the actions of Caliburn, We need to write the statement as follows.

Not clear. Hope you can advise me.

Next, we can operate on the interface. We create a new calculator. CS class, which is used to write action events.

We put two Textbox, three textblocks and one button in mainwindow. XAML. Caliburn is used to write the Click Event of the button.

Don't forget to add reference xmlns: Cal = "http://www.caliburnproject.org.

<Grid> <grid. rowdefinitions> <rowdefinition Height =" 32 * "/> <Rowdefinition Height =" 279 * "/> </Grid. rowdefinitions> <stackpanel orientation =" Horizontal "> <Textbox X: Name =" Left "Width =" 150 "/> <Textblock margin =" 5 0 ">/</Textblock> <textbox X: Name ="Right "Width =" 150 "/> <Textblock margin =" 5 0 ">=</Textblock> <textblock X: Name =" Result "Width =" 150 "/> </Stackpanel> <stackpanel grid. Row =" 1 "> <Button content =" Divide "Cal: Message. Attach =" Divide "/> </Stackpanel> </GRID>

In the calculator. CS class, we write the event functions processed by divide.

Public IntDivide (IntLeft,IntRight ){ReturnLeft/right ;}
 
Press F5 and you will see the following error:

There was no handler found for the message action: divide.

This means that no corresponding event processing is found, that is, no divide (IntLeft,IntRight) function.

Let's figure out why we didn't find it. Is the mainwindow. XAML page associated with the calculator. CS class?

Answer: apparently no.

Add in mainwindow. XAML

<CAL: Action. Target>

<Local: calculator/>

</CAL: Action. Target>

The local here is a reference of XAML to the local, such as xmlns: Local = "CLR-namespace: Action"

Now, no error is returned, but no result is returned because the event we write returns an int type data. Who will display this data?

The interface is not specified. So we modify CAL: Message. Attach ="DivideBecause the method is only applicable when no parameters are passed in or out.

Here, we need to input two parameters and return an int type data.

To:

Method 1:
 
CAL: Message. Attach ="Divide (left. Text, right. Text): result. Text"
 
Here, left, right, and result are the name of the control.
 
The operation is successful. Figure:
 
 

Why did we execute it when we clicked it, instead of executing this event when we move the mouse up.

What we write is exactly the following:

Method 2:
 
CAL: Message. Attach ="[Event click] = [Action divide (left. Text, right. Text): result. text]"

Click events are selected by default. Now you can use any event. So conciseCodeHow come we get it?

Method 3:
<Button content =" Divide (2) "> <CAL: Message. triggers> <CAL: routedmessagetriggercollection> <CAL: eventmessagetrigger eventname =" Click "> <CAL: eventmessagetrigger. Message> <CAL: actionmessage methodname =" Divide "Outcomepath =" Result. Text "> <CAL: parameter value =" {Binding elementname = left, Path = text} "/> <CAL: parameter value =" {Binding elementname = right, Path = text} "/> </CAL: actionmessage> </CAL: eventmessagetrigger. message> </CAL: eventmessagetrigger> </CAL: routedmessagetriggercollection> </CAL: Message. triggers> </button>  
Extension 1:

We know that there are 4 binding methods in the binding, such as twoway. Method 3 is very easy to write. How can we write in method 2?

The answer is as follows:

 
CAL: Message. Attach ="[Event click] = [Action divide (left. Text: twoway, right. Text: oneway): result. text]"

Add ":" and the binding method you want.

What should we do if we have multiple events in the button? For example, when a mouseenter event exists.

 
<Button content ="Divide"
CAL: Message. Attach ="[Event click] = [Action divide (left. Text: twoway, right. Text: oneway): result. Text];[Event mouseenter] = [Action Message]"/>The answer is: ";" can be followed by multiple events.
Extension 2:

Demand has been changing. We know that when division is performed, the denominator cannot be 0, but the above is not handled. If we enter 0 in the second Textbox,

An error is reported ~~

Let's add a judgment above.

[Preview ("Candivide")]Public IntDivide (IntLeft,IntRight ){ReturnLeft/right ;}Public BoolCandivide (IntLeft,IntRight ){ReturnRight! = 0 ;}

Running result:

Start:



After the input:



Enter 0. Do not click it.

Enter other numbers:

Executable.

Add [Preview ("Candivide")] Indicates that the function will be executed before it is executed.CandivideThis function.

If the function name is prefixed with CAN, Caliburn will execute it by default. That is to say, with candivide, the divide does not need to add [Preview ("Candivide,

Caliburn is automatically called by default.

Summarize the remaining issues:

Why is app. XAML. CS added?

    1. Caliburnframework. configurecore (). withpresentationframework (). Start (); how does it come from. If there is a pop-up form, do you want to use IOC injection?
    2. <CAL: Action. Target>

      <Local: calculator/>

      </CAL: Action. Target>

We reference the background method. Why should we reference it like this? If it is in mvvm mode, do you want to use it like this?

Hope you can give us some advice. Thank you.

Code:

Http://files.cnblogs.com/dingli/WPFCalliburn.rar

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.