Windows Phone 7 网络编程之留言板应用

简介:

 这个简易的留言板,是通过手机客户端与web程序的交互来设计的,保存留言的时候将数据传输到web,显示留言的时候再从数据库取数通过web传输到客户端。加强对HttpWebRequest异步请求的学习。

 

 

 


 
 
  1. <phone:PhoneApplicationPage   
  2.     x:Class="WindowsPhoneLiuyan.MainPage" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  9.     xmlns:Data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
  10.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="Portrait" Orientation="Portrait" 
  15.     shell:SystemTray.IsVisible="True"> 
  16.  
  17.     <!--LayoutRoot is the root grid where all page content is placed--> 
  18.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  19.         <Grid.RowDefinitions> 
  20.             <RowDefinition Height="Auto"/> 
  21.             <RowDefinition Height="*"/> 
  22.         </Grid.RowDefinitions> 
  23.  
  24.         <!--TitlePanel contains the name of the application and page title--> 
  25.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  26.             <TextBlock x:Name="PageTitle" Text="网络留言板" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  27.         </StackPanel> 
  28.  
  29.         <!--ContentPanel - place additional content here--> 
  30.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  31.             <Canvas  Height="600" Margin="0,4,6,4"> 
  32.                 <TextBlock x:Name="name1" Canvas.Top="327" Canvas.Left="8" Text="网   名:" Foreground="#FFF7F6F6"/> 
  33.                 <TextBox x:Name="name" Width="122" Canvas.Left="86" Canvas.Top="308" Background="#FFFBF6F6"/> 
  34.                 <TextBlock x:Name="message1" Canvas.Left="11" Canvas.Top="360" Text="留 言:" Foreground="#FFFBF9F9"/> 
  35.                 <TextBox x:Name="message" Width="321" Canvas.Left="86" Canvas.Top="360" VerticalScrollBarVisibility="Hidden" Height="124"/> 
  36.                 <Button Canvas.Left="30" Canvas.Top="490" Content="保存" Height="71" Name="save" Width="160" Click="save_Click" /> 
  37.                 <Button Canvas.Left="217" Canvas.Top="490" Content="重置" Height="71" Name="reset" Width="160" Click="reset_Click" /> 
  38.                 <TextBox Canvas.Left="-4" Canvas.Top="6" Height="296" Name="messages" Text="TextBox" Width="460" AcceptsReturn="True" /> 
  39.             </Canvas> 
  40.         </Grid> 
  41.     </Grid> 
  42. </phone:PhoneApplicationPage> 

 


 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using System.Text;  
  14. using System.IO;  
  15. using System.Collections.ObjectModel;  
  16.  
  17. namespace WindowsPhoneLiuyan  
  18. {  
  19.     public partial class MainPage : PhoneApplicationPage  
  20.     {  
  21.         public MainPage()  
  22.         {  
  23.             InitializeComponent();  
  24.             getMessage();  
  25.         }  
  26.  
  27.         private void save_Click(object sender, RoutedEventArgs e)  
  28.         {  
  29.             UriBuilder fullUri = new UriBuilder("http://localhost/liuyan/messege.ashx");  
  30.             fullUri.Query = "type=insert&name=" + name.Text + "&description=" + message.Text;  
  31.             // 创建WebRequest  
  32.             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri);  
  33.             // 创建同步的AsyncRequest  
  34.             UpdateState State = new UpdateState();  
  35.             State.AsyncRequest = myRequest;  
  36.             // 开始异步请求  
  37.             myRequest.BeginGetResponse(new AsyncCallback(HandleMyResponse), State);  
  38.         }  
  39.  
  40.         public void getMessage()  
  41.         {  
  42.             UriBuilder fullUri = new UriBuilder("http://localhost/liuyan/messege.ashx");  
  43.             fullUri.Query = "type=get";  
  44.             // 创建WebRequest  
  45.             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri);  
  46.             // 创建同步的AsyncRequest  
  47.             UpdateState State = new UpdateState();  
  48.             State.AsyncRequest = myRequest;  
  49.             // 开始异步请求  
  50.             myRequest.BeginGetResponse(new AsyncCallback(HandleMyResponse), State);  
  51.         }  
  52.  
  53.         private void HandleMyResponse(IAsyncResult asyncResult)  
  54.         {  
  55.             // 获取返回的信息  
  56.             UpdateState myState = (UpdateState)asyncResult.AsyncState;  
  57.             HttpWebRequest myRequest = (HttpWebRequest)myState.AsyncRequest;  
  58.             //结束异步请求  
  59.             myState.AsyncResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncResult);  
  60.             Stream streamResult = myState.AsyncResponse.GetResponseStream();  
  61.  
  62.             Deployment.Current.Dispatcher.BeginInvoke(() => 
  63.             {  
  64.                 if (streamResult.Length != 0)  
  65.                 {  
  66.                     StreamReader sr = new StreamReader(streamResult);  
  67.                     messages.Text = sr.ReadToEnd();  
  68.                 }  
  69.             });  
  70.         }  
  71.  
  72.         private void reset_Click(object sender, RoutedEventArgs e)  
  73.         {  
  74.             name.Text = "";  
  75.             message.Text = "";  
  76.         }  
  77.  
  78.     }  
  79.  
  80.     public class UpdateState  
  81.     {  
  82.         public HttpWebRequest AsyncRequest { get; set; }  
  83.         public HttpWebResponse AsyncResponse { get; set; }  
  84.     }  

web依然使用.NET

Message.ashx文件

 


 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data.OleDb;  
  6. using System.Data;  
  7.  
  8. namespace liuyan.Web  
  9. {  
  10.     /// <summary> 
  11.     /// Messege 的摘要说明  
  12.     /// </summary> 
  13.     public class Message : IHttpHandler  
  14.     {  
  15.  
  16.         public void ProcessRequest(HttpContext context)  
  17.         {  
  18.             context.Response.ContentType = "text/plain";  
  19.             string type = context.Request.QueryString["type"].ToString();  
  20.             switch (type)  
  21.             {  
  22.                 case "get":  
  23.                     get(context);  
  24.                     break;  
  25.                 case "insert":  
  26.                     insert(context);  
  27.                     break;  
  28.                 default:  
  29.                     break;  
  30.             }  
  31.         }  
  32.         /// <summary> 
  33.         /// 获取留言板数据  
  34.         /// </summary> 
  35.         /// <param name="context"></param> 
  36.         public void get(HttpContext context)  
  37.         {  
  38.             string messages = "";  
  39.             OleDbCommand cmd = new OleDbCommand();  
  40.             SQLExcute("SELECT * from about order by id desc", cmd);  
  41.             OleDbDataAdapter da = new OleDbDataAdapter();  
  42.             da.SelectCommand = cmd;  
  43.             DataSet ds = new DataSet();  
  44.             da.Fill(ds);  
  45.  
  46.             foreach (DataRow dr in ds.Tables[0].Rows)  
  47.             {  
  48.                 messages += dr[1] + " say:" + dr[2]+" ";  
  49.             }  
  50.  
  51.             context.Response.Write(messages.ToString());  
  52.         }  
  53.         /// <summary> 
  54.         /// 先往access数据库插入数据  然后再查询返回数据  
  55.         /// </summary> 
  56.         /// <param name="context"></param> 
  57.         public void insert(HttpContext context)  
  58.         {  
  59.             string name = context.Request.QueryString["name"].ToString();  
  60.             string description = context.Request.QueryString["description"].ToString();  
  61.             string sql = "insert into about(name,description) values('" + name + "','" + description + "')";  
  62.             SQLExcute(sql);  
  63.             string messages = "";  
  64.             OleDbCommand cmd = new OleDbCommand();  
  65.             SQLExcute("SELECT * from about order by id desc", cmd);  
  66.             OleDbDataAdapter da = new OleDbDataAdapter();  
  67.             da.SelectCommand = cmd;  
  68.             DataSet ds = new DataSet();  
  69.             da.Fill(ds);  
  70.  
  71.             foreach (DataRow dr in ds.Tables[0].Rows)  
  72.             {  
  73.                 messages += "("+dr[1]+")" + " say:" + dr[2];  
  74.             }  
  75.  
  76.             context.Response.Write(messages.ToString());  
  77.         }  
  78.         //SQL的操作     
  79.         private void SQLExcute(string SQLCmd)  
  80.         {  
  81.             string ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=D:\\code\\Message\\App_Data\\information.mdb";  
  82.             OleDbConnection conn = new OleDbConnection(ConnectionString);  
  83.             conn.Open();  
  84.             OleDbCommand cmd = new OleDbCommand();  
  85.             cmd.Connection = conn;  
  86.             cmd.CommandTimeout = 15;  
  87.             cmd.CommandType = CommandType.Text;  
  88.             cmd.CommandText = SQLCmd;  
  89.             cmd.ExecuteNonQuery();  
  90.             conn.Close();  
  91.         }  
  92.         //SQL的操作 是SQLExcute的重构  
  93.         private void SQLExcute(string SQLCmd, OleDbCommand Cmd)  
  94.         {  
  95.             string ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=D:\\code\\Message\\App_Data\\information.mdb";  
  96.             OleDbCommand cmd = new OleDbCommand();  
  97.             OleDbConnection Conn = new OleDbConnection(ConnectionString);  
  98.             Conn.Open();  
  99.             Cmd.Connection = Conn;  
  100.             Cmd.CommandTimeout = 15;  
  101.             Cmd.CommandType = CommandType.Text;  
  102.             Cmd.CommandText = SQLCmd;  
  103.             Cmd.ExecuteNonQuery();  
  104.             Conn.Close();  
  105.         }  
  106.         public bool IsReusable  
  107.         {  
  108.             get  
  109.             {  
  110.                 return false;  
  111.             }  
  112.         }  
  113.     }  

 



本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078692

相关文章
|
1月前
|
机器学习/深度学习 PyTorch TensorFlow
卷积神经网络深度解析:从基础原理到实战应用的完整指南
蒋星熠Jaxonic,深度学习探索者。深耕TensorFlow与PyTorch,分享框架对比、性能优化与实战经验,助力技术进阶。
|
3月前
|
监控 安全 Shell
管道符在渗透测试与网络安全中的全面应用指南
管道符是渗透测试与网络安全中的关键工具,既可用于高效系统管理,也可能被攻击者利用实施命令注入、权限提升、数据外泄等攻击。本文全面解析管道符的基础原理、实战应用与防御策略,涵盖Windows与Linux系统差异、攻击技术示例及检测手段,帮助安全人员掌握其利用方式与防护措施,提升系统安全性。
190 6
|
7月前
|
SQL 分布式计算 Serverless
鹰角网络:EMR Serverless Spark 在《明日方舟》游戏业务的应用
鹰角网络为应对游戏业务高频活动带来的数据潮汐、资源弹性及稳定性需求,采用阿里云 EMR Serverless Spark 替代原有架构。迁移后实现研发效率提升,支持业务快速发展、计算效率提升,增强SLA保障,稳定性提升,降低运维成本,并支撑全球化数据架构部署。
759 56
鹰角网络:EMR Serverless Spark 在《明日方舟》游戏业务的应用
|
1月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
103 11
|
6月前
|
人工智能 监控 安全
NTP网络子钟的技术架构与行业应用解析
在数字化与智能化时代,时间同步精度至关重要。西安同步电子科技有限公司专注时间频率领域,以“同步天下”品牌提供可靠解决方案。其明星产品SYN6109型NTP网络子钟基于网络时间协议,实现高精度时间同步,广泛应用于考场、医院、智慧场景等领域。公司坚持技术创新,产品通过权威认证,未来将结合5G、物联网等技术推动行业进步,引领精准时间管理新时代。
|
2月前
|
机器学习/深度学习 人工智能 算法
卷积神经网络深度解析:从基础原理到实战应用的完整指南
蒋星熠Jaxonic带你深入卷积神经网络(CNN)核心技术,从生物启发到数学原理,详解ResNet、注意力机制与模型优化,探索视觉智能的演进之路。
387 11
|
7月前
|
存储 SQL 运维
中国联通网络资源湖仓一体应用实践
本文分享了中国联通技术专家李晓昱在Flink Forward Asia 2024上的演讲,介绍如何借助Flink+Paimon湖仓一体架构解决传统数仓处理百亿级数据的瓶颈。内容涵盖网络资源中心概况、现有挑战、新架构设计及实施效果。新方案实现了数据一致性100%,同步延迟从3小时降至3分钟,存储成本降低50%,为通信行业提供了高效的数据管理范例。未来将深化流式数仓与智能运维融合,推动数字化升级。
329 0
中国联通网络资源湖仓一体应用实践
|
9月前
|
机器学习/深度学习 编解码 自动驾驶
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
366 3
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
|
3月前
|
数据采集 存储 数据可视化
Python网络爬虫在环境保护中的应用:污染源监测数据抓取与分析
在环保领域,数据是决策基础,但分散在多个平台,获取困难。Python网络爬虫技术灵活高效,可自动化抓取空气质量、水质、污染源等数据,实现多平台整合、实时更新、结构化存储与异常预警。本文详解爬虫实战应用,涵盖技术选型、代码实现、反爬策略与数据分析,助力环保数据高效利用。
247 0
|
3月前
|
安全 Linux
利用Libevent在CentOS 7上打造异步网络应用
总结以上步骤,您可以在CentOS 7系统上,使用Libevent有效地构建和运行异步网络应用。通过采取正确的架构和代码设计策略,能保证网络应用的高效性和稳定性。
131 0
下一篇
oss云网关配置