Document directory
- 1. Windows Phone 7 peripherals
- 2. Implement gravity sensing for Windows Phone 7
Windows Phone 7 peripherals and gravity sensing
Note: Only the radio and vibration functions of Windows Phone are introduced here. (Refer to the video of Chuanzhi blog)
I. Windows Phone 7 peripherals 1.1 realization of the radio function of Windows Phone 7
To implement the radio function, you must first reference this namespace:
using Microsoft.Devices.Radio;
Before implementing the radio function, we need to know that fmradio is a singleton class, that is, it only has one instance, so it is also in singleton mode. We can no longer declare an instance.
Note: Singleton mode:The Singleton Mode means that there is only one instance. The Singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. This class is called a singleton class.
The only instance in singleton mode is instance. It has the following attributes:
Currentregion |
Set the region where the radio is located. Currently, only the United States, Japan, and Europe are supported. |
Frequency |
Set the frequency of the radio |
Powermode |
Set the power mode of the radio, on or off) |
Signalstrength |
Indicates the signal strength of the radio. |
Fmradio has two enumerations:
Radiopowermode |
Indicates the power mode, which has two enumerated values: On and off. |
Radioregion |
Indicates the region where the radio is located. There are three enumerated values: Europe, Japan, and UnitedStates. |
Next we will look at how to implement the radio function through code.
First, we set a variable of fmradio which is equal to the instance.
FMRadio myradio= FMRadio.Instance;
Then set the variable (that is,Instance), The power mode is on (that is, turn on the radio ):
Myradio. powermode = radiopowermode. On; // set the power mode to on.
You can also set the regional frequency of the radio:
Myradio. currentregion = radioregion. UnitedStates; // set the radio location to usmyradio. frequency = 100; // set the radio frequency to 100.
After the settings are complete, the basic functions of a radio are implemented.
1.2 vibration implementation for Windows Phone 7
The namespace must be referenced to achieve the vibration effect.
using Microsoft.Phone.Devices;
To achieve vibration, you only need one sentence:
VibrateController.Default.Start(TimeSpan.FromSeconds(2));
This line of code indicates that the vibration is measured in seconds (fromseconds), and the vibration is two seconds. Similarly, fromdays, fromhours, fromminutes, and so on.
2. Implement gravity sensing for Windows Phone 7
First, you must reference this namespace:
Using Microsoft.Devices.Sensors;
Then we instantiate an object:
Accelerometer acc = new Accelerometer();
Then register the event readingchanged to detect acceleration changes:
ACC. readingchanged + = new eventhandler <accelerometerreadingeventargs> (acc_readingchanged); // register event acc. Start (); // start gravity Acceleration
When the acceleration changes, the readingchanged event is triggered, however, because the calling event processing function and the currently running page are in different threads (the gravity-sensing event processing function runs in the background), you need to use. windows. the dispatcher class in the deployment namespace to call the method of the thread located on the current page. The Code is as follows:
void acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e) { this.Dispatcher.BeginInvoke(new accdelegete(ACCchanged), e); }
Declare a delegate:
public delegate void accdelegete (AccelerometerReadingEventArgse);
Declare a delegate proxy method:
Void accchanged (accelerometerreadingeventargs e) {// Add the code to be executed when the acceleration changes (that is, the desired function) xtextbox. TEXT = E. x. tostring (); // assign the gravity sensor data in the X axis to textbox ytextbox. TEXT = E. y. tostring (); // assign the gravity sensor data in the Y axis to textbox ztextbox. TEXT = E. z. tostring (); // assign the gravity sensor data in the Z axis to textbox}
In this way, the application of gravity acceleration sensing is realized.
(All Rights Reserved. For details, refer to the source)