. NET Compact framework BASICS (1)

Source: Internet
Author: User
Tags compact

I always wanted to sort out the usage and unmanaged methods of all managed classes under all. NET Compact framework.

Write thisArticlePurpose: To help developers who want to get started and use some functions as a reference document. When writing these articles, I may be constantly learning and not very familiar with a lot of knowledge. When reading these articles, you can tell me your own needs and ideas, I will continue to expand and improve it.

The article begins with the Managed class.

Development Environment: vs2008, platform: wm6.0 professional

This article describes three namespaces:
1. Microsoft. windowsmobile. Status (Microsoft. windowsmobile. Status. dll)
2. Microsoft. windowsmobile. telephony (Microsoft. windowsmobile. telephony. dll)
3. Microsoft. Win32 (mscorlib. dll)

1. Microsoft. windowsmobile. Status has two main classes
Systemstate: used to collect and monitor many status information of the system, including calendar, email, phone, synchronization, power supply, etc.
Registrystate: used to monitor a key value in the registry.
In addition, the namespace also contains six enumerations.
Activesyncstatus: synchronization status of the device (Synchronization in progress, no synchronization, synchronization error)
Batterylevel: battery level (veryhigh (80%-100%), high (60%-80%), nomail (20%-60%), low (10%-20% ), verylow (0%-10% ))
Batterystate: Battery status (critical, low, nomail)
Lockstates: devicelock, keylock, none, simlock)
Systemproperty: All information used for monitoring in systemstate.

Demo1:
Monitor battery information: triggered when the battery level changes.

Figure 1: The initial interface after the demo is run. When you click "Current status", the current battery level is displayed.

Figure 2: how to change the battery grade.

Figure 3: immediate trigger event, which is defined as displaying the new battery level.

MainCode:

1 Private   Void Statusform_load ( Object Sender, eventargs E)
2 {
3 Status =   New Systemstate (systemproperty. powerbatterystrength );
4 Status. Changed + = New Changeeventhandler (status_changed );
5
6 Regstatus =   New Registrystate ( @" HKEY_CURRENT_USER \ My User Key " , " My key value " );
7 Regstatus. Changed + =   New Changeeventhandler (regstatus_changed );
8 }

Status is the system status object. It is initialized in form_load and its change event is registered. Once changed, it is triggered.

1 This . Listbox1.items. Add ( " Current value: "   + (Batterylevel) systemstate. getvalue (systemproperty. powerbatterystrength). tostring ());

Obtain battery information in the current status event. Use getvalue.

1 Private   Void Status_changed ( Object Sender, changeeventargs Arg)
2 {
3This. Listbox1.items. Add ("New value:" +(Batterylevel) Arg. newvalue). tostring ());
4}

A registration event is triggered when a change occurs.

Demo2: monitor registry changes
In the mobile device registry, there are four categories: classesroot, localmachine, currentuser, and users.
In Demo, the monitored key value is my key value under HKEY_CURRENT_USER \ My User Key.

Click "Change Registry" in Figure 3 to modify the key value. Figure 4 shows the monitoring effect.

Code:

1 Private   Void Statusform_load ( Object Sender, eventargs E)
2 {
3 Status =   New Systemstate (systemproperty. powerbatterystrength );
4 Status. Changed + = New Changeeventhandler (status_changed );
5
6 Regstatus =   New Registrystate ( @" HKEY_CURRENT_USER \ My User Key " , " My key value " );
7 Regstatus. Changed + =   New Changeeventhandler (regstatus_changed );
8 }

Register a monitoring event

1 Private   Void Regstatus_changed ( Object Sender, changeeventargs ARGs)
2 {
3This. Listbox1.items. Add ("Registry new value:" +Args. newvalue );
4}

Trigger event processing

2. Microsoft. windowsmobile. telephony only has the phone class.
This class provides a direct pop-up call interface. This class provides the talk method. This method is used to dial a phone number (string type.

Code:

1 Private   Void Menuitem5_click ( Object Sender, eventargs E)
2 {< br> 3 phone = New phone ();
4 phone. talk ( " 7272024 " , true );
5 }

Call 7272024. The second parameter indicates whether a prompt is displayed.

Figure 5 effect: If yes is selected, the dialing page is displayed. This interface is not displayed if this parameter can be left blank.


3. Microsoft. Win32
Provides two classes: Registry and registrykey.
Provides four enumeration types: registryhive, registrykeypermissioncheck, registryvaluekind, and registryvalueoptions.
This class is used in the same way as that in the complete framework.

Code in this demo:

1 Registrykey key = Registry. currentuser;
2 Registrykey regmykey = Key. opensubkey (sub_key, True );
3 If (Regmykey ! =   Null )
4 {
5 If (Regmykey. getvalue (sub_key_name). tostring () =   " 1 " )
6 Regmykey. setvalue (sub_key_name, " 0 " , Registryvaluekind. String );
7 Else
8 Regmykey. setvalue (sub_key_name, " 1 " );
9 }
10 Else
11 {< br> 12 key = key. createsubkey (sub_key);
13 key. setvalue (sub_key_name, " 1 " , registryvaluekind. string);
14 }

Note: The second parameter of the opensubkey method. If this parameter is not set, the default value is false, and the opened key is read-only. You cannot call setvalue to modify the key value.

Summary:
After all,. Net CF provides limited hosting code. After reading this article, all beginners hope to help.

Download Code: deviceapidemo.rar (152.64 KB)

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.