当前位置:首 页 > 攻城湿 >Windows Phone > 查看文章

Windows Phone 7 自定义事件

Windows Phone 你是第2218个围观者 2条评论 供稿者: 标签:, , , , , ,

在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"/> 
        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"></button> 
            <textblock Height="50" HorizontalAlignment="Left" Margin="96,270,0,0" Name="textBlock1" Text="网络的状态:" VerticalAlignment="Top" Width="126"></textblock> 
            <textblock Height="48" HorizontalAlignment="Left" Margin="34,326,0,0" Name="textBlock2" Text="" VerticalAlignment="Top" Width="377"></textblock> 
        </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();  
        }  
    }  
} 

运行的效果如下:
WindowsPhoneCustomizedEvent

这家伙很懒,什么都没写!

—— zhaorong

zhaorong
你可能也喜欢Related Posts
众说纷纭Comments
大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头
小提示:直接粘贴图片到输入框试试
努力发送中...
  1. 1 楼 Carte R4

    Once again another great entry. I actually have a few things to ask you, would be have some time to answer them?

    2013年03月23日 10:33:07 回复 取消回复
  2. 2 楼 maillot de foot pas cher

    I like the worthwhile info you supply within your posts.I

    2013年03月25日 08:53:10 回复 取消回复
  • 评论最多
  • 最新评论
  • 随机文章
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容
Copyright © zhaorong All Rights Reserved. 滇ICP备15006105号-1