Mgen spa Engineering 2 tutorial (Part 1): List controls

Source: Internet
Author: User

Back to the tutorial directory

 

The SPA project supports a variety of list controls. They have:

Single choice ListBox:

Single-choice radiobutton list:

Single choice ComboBox:

Select multiple ListBox:

Multiple-choice checkbox list:

 

Next I will start to explain the list controls of the SPA project.

Directory

  • 1. Single-choice control for strings: selectorstring Control
  • 2. Multi-choice control for string array: multiselectorstrings Control
  • 3. Single-choice control for enumeration: selectorenum Control
  • 4. multiple-choice control for enumeration: multiselectorflagenum Control

 

Returned directory
1. Single-choice control for strings: selectorstring Control

Since it is for strings, you can guess that this selector control will obviously assign the selected option value directly to the core data behind it. In addition, the initialization of this control (that is, the data source of the list control) is also completed through a string array. The advantage of this control is that it is convenient and simple, because the initialization data in the code will be directly displayed on the interface, and the text on the interface will be directly assigned to the back-end data. The disadvantage is also obvious. It is precisely because this interface and the core use the same data set mechanism that you cannot use in a multi-language environment. Of course, this problem will be solved later when selecting controls for enumeration.

 

This is the definition of the selectorstring control constructor:

Public selectorstringcontrolattribute (selectortype type, string [] source)

 

The first parameter is the selectortype enumeration. The enumerated value has a very simple meaning:

ListBox

ComboBox

Radiobuttonlist

From the name, we can see the different styles of the single-choice selector. There is a legend at the beginning of the article, so we will not introduce it here.

 

The second parameter of the constructor is the string array used as the selector data source.

 

Let's define a type for the selectorstring control:

Class myspatype

{

[Spaheader ("key1")]

[Selectorstringcontrol (selectortype. radiobuttonlist, new string [] {"mgen", "Tony", "Martin"})]

Public String data {Get; set ;}

}

 

After the page is generated:

(If no selection is made at this time, the SPA project will prompt an error, and the data property will still retain the default value null .)

 

(After a project is selected, the value of the data attribute is the value of the selected item. For example, the value of the data attribute is the string "mgen ".)

 

Returned directory
2. Multi-choice control for string array: multiselectorstrings Control

This control has the same running mechanism as the single-choice control for strings mentioned above, except that it supports multiple-choice. Therefore, the core type it targets cannot be a string, but a string array, representing all selected items.

The constructor is similar to the preceding control:

Public multiselectorstringscontrolattribute (multiselectortype type, string [] source)

The first parameter is the control style, and the second parameter is the data source.

Multiselectortype enumeration can have the following values:

ListBox

Checkboxlist

Similarly, there is a legend at the top of the article, so I won't talk about it more here.

 

Example Type Definition:

Class myspatype

{

[Spaheader ("key1")]

[Multiselectorstringscontrol (multiselectortype. checkboxlist, new string [] {"mgen", "Tony", "Martin"})]

Public String [] data {Get; set ;}

}

 

Run:

(The value of the data attribute is a string array with two elements: "mgen" and "Martin ".)

 

Returned directory
3. Single-choice control for enumeration: selectorenum Control

After the selector control for the string is finished, you will find that its working principle is very simple. The disadvantage is fatal:

1. multiple languages are not supported.

2. In actual programming, the data type with the selected nature is usually enumeration, rather than a set of strings.

 

OK. The selector for Enumeration type does not have the above problem. Let's first look at the selectorenum radio control for enumeration.

 

Since it is for enumeration, after selecting the selector option, the attribute is directly assigned to the enumeration value. The difficulty in selecting controls for enumeration is the initialization of controls. That is to say, "How to display enumeration as multi-language text ?", The answer is resource storage in the interface generation option described in the second part of the tutorial.

 

Let's take a quick look at the relevant content. resource storage is defined in the resource attribute of the spaobjectoption object generated on the interface. The corresponding type is the iresourceprovider interface, which defines two methods: getstring and getenum. The implementation requires that the text of the current language be returned Based on the Resource Key (string) or enumeration.

What readers know about resource storage is the spaheader feature and the spagroupheader feature described in the previous section. They all use the iresourceprovider's getstring method. Now, the Selection Control for enumeration will use another iresourceprovider method: getenum.

 

Don't forget that the SPA project also provides the default execution of an iresourceprovider: defaultresprovider type, which is constructed by a resx file type. getstring directly returns the key value in resx, the getenum method returns the value from the resx resource file as a key based on "[Enumeration type name] _ [enumeration value.

 

OK. We can define an enumeration as follows:

Enum country

{

China, USA, and Japan

}

 

In this way, define the enumerated value text in the resource files of different languages:

 

Next, define the type (note that the control will automatically initialize the property Enumeration type based on the property definition, and the developer does not need to specify the enumeration type ):

Class myspatype

{

[Spaheader ("key1")]

[Selectorenumcontrol (selectortype. radiobuttonlist)]

Public Country Data {Get; set ;}

}

 

The last generation interface supports multiple languages. The code is actually the same as the code for the third part about Hello world. Here we will post it again:

// + Using mgen. spa;

// + Using mgen. spa. FX;

 

// Create an object

Myspatype myobject = new myspatype ();

// Delegate the logic displayed on the Interface

Action showaction = () =>

{

Spaobjectoption opt = new spaobjectoption (

// Create verification options

New validationoption (

// Verify as the control changes

Validationtrigger. propertychanged,

// Perform verification after loading

Onloadvalidation. required,

// Do not read object values

Objectoption. New ),

// Create resource storage

// The res type is the res. resx resource file in the project.

New defaultresprovider (typeof (RES )),

// Define the title attribute

"Mgen SPA project V2 ");

 

New spawindow (spamanager. GetObject (myobject, OPT). Show ();

};

// Set the thread currentuiculture to Chinese

System. Threading. thread. currentthread. currentuiculture = new system. Globalization. cultureinfo ("ZH-CN ");

Showaction ();

// Set the thread currentuiculture to Chinese

System. Threading. thread. currentthread. currentuiculture = new system. Globalization. cultureinfo ("En-us ");

Showaction ();

 

Running result:

 

Each time you select an option, the attribute value is changed to the corresponding enumerated value.

 

 

Returned directory
4. multiple-choice control for enumeration: multiselectorflagenum Control

After talking about the Single-choice controls for enumeration, there is actually not much to talk about the multiple-choice controls. There are only two things to talk about.

The first point is that the list type of the multiselectorflagenum control and the list type of the multiselectorstrings control are the same, all of which are the multiselectortype Enumeration type.

The second point is the name of the multiselectorflagenum control, which is of the "flagenum" type ". That is, enumeration defined by bits. When multiple options are selected, the SPA project uses the "|" operator to combine multiple enumerated values.

 

In this way, we can modify the country Enumeration type so that each of its enumerated values represents a bit:

[Flags]

Enum country

{

China = 1, USA = 2, Japan = 4

}

 

Type Definition:

Class myspatype

{

[Spaheader ("key1")]

[Multiselectorflagenumcontrol (multiselectortype. checkboxlist)]

Public Country Data {Get; set ;}

}

 

Run:

The attribute data value is:

Country. China | country. USA

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.