公司做网站需要什么,怎样做网站导航栏,wordpress 排版不正常,wordpress怎么导入织梦在Windows Phone的应用开发里面#xff0c;对于事件这种东西我们可以随处可见#xff0c;系统本来就已经封装好了各种各样的事件机制#xff0c;如按钮的单击事件等等的。在实际的开发中#xff0c;我们需要自己去给相关的类自定义一些事件来满足业务的要求#xff0c;特别… 在Windows Phone的应用开发里面对于事件这种东西我们可以随处可见系统本来就已经封装好了各种各样的事件机制如按钮的单击事件等等的。在实际的开发中我们需要自己去给相关的类自定义一些事件来满足业务的要求特别在使用观察着模式的时候在wp7中利用事件去实现是理所当然的。 自定义事件步骤有下面的几个步骤 1、继承EventArgs类实现自己自定义的事件参数 2、定义一个委托 3、定义一个事件 4、添加事件。 下面来看一下一个Demo对自定义事件的实现这个Demo只是对网络请求的状态进行一个简单的事件监控的调用处理 自定义的事件参数类 StateChangedEventArgs.cs using System;namespace EventDemo{/// summary/// 状态事件/// /summary public class StateChangedEventArgs : EventArgs {public readonly string NewState;public readonly DateTime Timestamp;public StateChangedEventArgs(string newstate) {this.NewState newstate;this.Timestamp DateTime.Now; } }} 在业务类里面定义事件 NetTask.cs using System;using System.Net;using System.Threading;using System.IO;namespace EventDemo{public class NetTask {//定义委托 public delegate void StateChanged(NetTask sender, StateChangedEventArgs args);//定义事件 public event StateChanged OnStateChanged;//出事状态 public string NetTaskName ;/// summary/// 网络任务/// /summary/// param nameurl/param public void StartNetTask(string url) {bool success false;int attempt 0;while (attempt 3) { AsyncCallback callback null;//开启线程等待 ManualResetEvent webRequestWait new ManualResetEvent(false); Uri targetUri new Uri(url); HttpWebRequest request (HttpWebRequest)WebRequest.Create(targetUri); request.Method POST;if (callback null) { callback delegate(IAsyncResult asRequest) {try { success true; webRequestWait.Set();//…… }catch { OnStateChanged(this, new StateChangedEventArgs(重试)); webRequestWait.Set(); } }; } request.BeginGetRequestStream(callback, request);//等待线程结束 webRequestWait.WaitOne();if (success) {break; } attempt; Thread.Sleep(1000); }if (success) { OnStateChanged(this, new StateChangedEventArgs(成功)); Thread.Sleep(50); }else { OnStateChanged(this, new StateChangedEventArgs(失败)); } } }} 简单的测试一下 Grid x:NameContentPanel Grid.Row1 Margin12,0,12,0Button Content测试网络 Height72 HorizontalAlignmentLeft Margin143,105,0,0 Namebutton1 VerticalAlignmentTop Width202 Clickbutton1_Click /TextBlock Height50 HorizontalAlignmentLeft Margin96,270,0,0 NametextBlock1 Text网络的状态 VerticalAlignmentTop Width126 /TextBlock Height48 HorizontalAlignmentLeft Margin34,326,0,0 NametextBlock2 Text VerticalAlignmentTop Width377 //Grid MainPage.xaml.cs using System.Windows;using Microsoft.Phone.Controls;namespace EventDemo{public partial class MainPage : PhoneApplicationPage {public MainPage() { InitializeComponent(); }private void button1_Click(object sender, RoutedEventArgs e) { NetTask netTask new NetTask(); netTask.OnStateChanged OnStateChanged; netTask.NetTaskName 测试网络; netTask.StartNetTask(http://www.cnblogs.com); }public void OnStateChanged(object sender, StateChangedEventArgs e) { NetTask temp sender as NetTask; textBlock2.Text temp.NetTaskName , e.NewState,e.Timestamp.ToLongTimeString(); } }} 运行的效果如下 转载于:https://www.cnblogs.com/linzheng/archive/2012/03/26/2418811.html