ArticleDirectory
- Interface Creation
- Detailed code implementation
Beetle provides the implementation of beetle. SL Based on Silverlight. Because WP7 Development also provides socket support, beetle. SL can be fully used in WP7. Protobuf.net also supports WP7. Therefore, you can use beetle. SL + protobuf.net to implement object-based TCP communication applications in WP7.
First create a WP project, reference beetle. WP. dll, beetle. WP. protobuf. dll and protobuf-net.DLL respectively; the above components can be divided into other.
Interface Creation
The main function is to query the customer information after logging in. You can enter the name of the customer to be queried.
Details Code Implementation
Create a protobuf-based TCP connection andProgramBinding related events during loading
Private beetle. protobufadapter. protobufchannel mchannel = new Beetle. protobufadapter. protobufchannel (); Private void phoneapplicationpage_loaded (Object sender, routedeventargs e) {messageadapter. loadmessage (GetType (). assembly); mchannel. connected + = onconnected; mchannel. disposed + = ondisposed; mchannel. receive + = onreceive; mchannel. error + = onerror ;}
The connection button event is used to establish a TCP connection. After the connection is successful, the query button becomes available.
Private void initialize connect_click (Object sender, routedeventargs e) {mchannel. connect (txtipaddress. text, 4520);} private void onconnected (Object sender, eventchannelarges e) {This. dispatcher. begininvoke () => {consumer connect. isenabled = false; elastic search. isenabled = true; txtstatus. TEXT = "connected ";});}
In the query button, perform a query object query and bind data in the event reception.
Private void initialize search_click (Object sender, routedeventargs e) {packages. search search = new packages. search (); search. companyName = txtcustomername. text; messageadapter. send (mchannel, search);} private void onreceive (Object sender, eventchannelreceiveargs e) {messageadapter adapter = (messageadapter) E. message; If (adapter. message is packages. searchresult) {packages. searchresult result = (packages. searchresult) adapter. message; this. dispatcher. begininvoke () => {lstcustomers. itemssource = result. MERs MERS ;});}}
This simple user query is complete. It is very easy to use beetle for TCP communication in Windows Phone :)
If you need it, you can go to the http://beetlesl.codeplex.com/get the relevant code