欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Windows Phone 7 自定义事件

程序员文章站 2023-01-22 20:33:40
  在windows phone的应用开发里面,对于事件这种东西我们可以随处可见,本来就已经封装好了各种各样的事件机制,如按钮的单击事件等等的。在实际的开发中,我们需要自己去给相关的类自定...

  在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("");
        }

        public void onstatechanged(object sender, statechangedeventargs e)
        {
            nettask temp = sender as nettask;
            textblock2.text = temp.nettaskname + "," + e.newstate+","+e.timestamp.tolongtimestring();
        }
    }
}

 运行的效果如下:

  Windows Phone 7 自定义事件


摘自  linzheng