Wp7可以方便得调用.net的WebService
先用C#做一个WebService,实现两个接口
GetMessage :发一条信息
SendMessage :接受最新一条信息
- using System;
- using System.Data;
- using System.Web;
- using System.Collections;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.ComponentModel;
- using System.Data.SqlClient;
- using COM;
- namespace WindowsService
- {
- /// <summary>
- /// Service1 の概要の説明です
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ToolboxItem(false)]
- public class Service1 : System.Web.Services.WebService
- {
- [WebMethod]
- public Message GetMessage(String id)
- {
- SqlConnection SqlCon = new SqlConnection();
- DataSet ds = new DataSet();
- Message msg = null;
- try
- {
- SqlCon = DBUtil.getSqlConnection();
- DBUtil.SelectSqlCommand(ds, "select TOP 1 body,sms_time from SMSTbl where id = '" + id + "' order by sms_time DESC", ref SqlCon);
- if (ds.Tables[0].Rows.Count > 0)
- {
- msg = new Message();
- msg.body = ds.Tables[0].Rows[0][0].ToString();
- msg.time = ds.Tables[0].Rows[0][1].ToString();
- }
- return msg;
- }
- finally
- {
- DBUtil.closeConnection(SqlCon);
- }
- }
- [WebMethod]
- public int SendMessage(String id, String body)
- {
- SqlConnection SqlCon = new SqlConnection();
- try
- {
- SqlCon = DBUtil.getSqlConnection();
- return DBUtil.InsertSqlCommand("Insert into SMSTbl( id,sms_time,body) values('" + id + "',getdate(),'" + body + "')", ref SqlCon);
- }
- finally
- {
- DBUtil.closeConnection(SqlCon);
- }
- }
- }
- }
再在wp7 程序引用WebService
在wp7里调用它
- //添加引用
- using DecoMailer.ServiceReference1;
- //在[按钮]押下事件中
- private void KanjiModeButton_Click(object sender, RoutedEventArgs e)
- {
- //实例化SoapClient
- ServiceReference1.Service1SoapClient soap = new Service1SoapClient();
- //调用GetMessage方法,并把ID:1传给方法
- soap.GetMessageAsync("1");
- //Respones将触发GetMessageCompletedEvent事件,并调用soap_GetMessage处理
- soap.GetMessageCompleted += new EventHandler<GetMessageCompletedEventArgs>(soap_GetMessage);
- }
- private void soap_GetMessage(object sender, GetMessageCompletedEventArgs e)
- {
- if (e.Error == null)
- {
- //取得最新的Message类对象
- ServiceReference1.Message lastMessage = e.Result;
- //MessageEditor.Message = lastMessage;
- }
- }