在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 name="url"></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:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Button Content="测试网络" Height="72" HorizontalAlignment="Left" Margin="143,105,0,0" Name="button1" VerticalAlignment="Top" Width="202" Click="button1_Click" />
- <TextBlock Height="50" HorizontalAlignment="Left" Margin="96,270,0,0" Name="textBlock1" Text="网络的状态:" VerticalAlignment="Top" Width="126" />
- <TextBlock Height="48" HorizontalAlignment="Left" Margin="34,326,0,0" Name="textBlock2" Text="" VerticalAlignment="Top" Width="377" />
- </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();
- }
- }
- }
运行的效果如下:
原文链接:http://www.cnblogs.com/linzheng/archive/2012/03/26/2418811.html