微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wp7 调用WebService

Wp7可以方便得调用.net的WebService

 

先用C#做一个WebService,实现两个接口

GetMessage :发一条信息

SendMessage :接受最新一条信息

@H_502_18@ [c-sharp]  view plain copy
  1. using System;  
  2. using System.Data;  
  3. using System.Web;  
  4. using System.Collections;  
  5. using System.Web.Services;  
  6. using System.Web.Services.Protocols;  
  7. using System.ComponentModel;  
  8. using System.Data.sqlClient;  
  9. using COM;  
  10.   
  11. namespace WindowsService  
  12. {  
  13.     /// <summary>  
  14.     /// Service1 の概要の説明です  
  15. /// </summary>  
  16.     [WebService(Namespace = "http://tempuri.org/")]  
  17.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  18.     [ToolBoxItem(false)]  
  19.     public class Service1 : System.Web.Services.WebService  
  20.     {  
  21.   
  22.         [WebMethod]  
  23.         public Message GetMessage(String id)  
  24.         {  
  25.             sqlConnection sqlCon = new sqlConnection();  
  26.             DataSet ds = new DataSet();  
  27.             Message msg = null;  
  28.             try  
  29.             {  
  30.                 sqlCon = DBUtil.getsqlConnection();  
  31.                 DBUtil.SelectsqlCommand(ds, "select TOP 1 body,sms_time from SMSTbl where id = '" + id + "' order by sms_time DESC"ref sqlCon);  
  32.                 if (ds.Tables[0].Rows.Count > 0)  
  33.                 {  
  34.                     msg = new Message();  
  35.                     msg.body = ds.Tables[0].Rows[0][0].ToString();  
  36.                     msg.time = ds.Tables[0].Rows[0][1].ToString();  
  37.                 }  
  38. return msg;  
  39.             }  
  40. finally  
  41.                 DBUtil.closeConnection(sqlCon);  
  42.         }  
  43. int SendMessage(String id, String body)  
  44.                 return DBUtil.InsertsqlCommand("Insert into SMSTbl( id,sms_time,body) values('" + id + "',getdate(),'" + body + "')",248); line-height:18px">             }  
  45.             finally  
  46.             {  
  47.                 DBUtil.closeConnection(sqlCon);  
  48.         }  
  49.     }  
  50. }  

再在wp7 程序引用WebService

在wp7里调用

copy

    //添加引用  
  1. using DecoMailer.ServiceReference1;  
  2. //在[按钮]押下事件中         
  3.  private void KanjiModeButton_Click(object sender, RoutedEventArgs e)  
  4. //实例化SoapClient  
  5.             ServiceReference1.Service1SoapClient soap = new Service1SoapClient();  
  6. //调用GetMessage方法,并把ID:1传给方法  
  7.             soap.GetMessageAsync("1");  
  8. //Respones将触发GetMessageCompletedEvent事件,并调用soap_GetMessage处理  
  9.             soap.GetMessageCompleted += new EventHandler<GetMessageCompletedEventArgs>(soap_GetMessage);  
  10. void soap_GetMessage(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; color:rgb(0, GetMessageCompletedEventArgs e)  
  11. if (e.Error == null)  
  12. //取得最新的Message类对象  
  13.                 ServiceReference1.Message lastMessage = e.Result;  
  14.                 //MessageEditor.Message = lastMessage;  
  15.         }  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐