Windows Phone 7 custom events

Source: Internet
Author: User

In Windows Phone application development, events can be seen everywhere. The system has encapsulated various event mechanisms, such as button-clicking events. In actual development, we need to customize some events for the relevant classes to meet the business requirements, especially when using the observation mode, using events in WP7 is a matter of course.

The steps for custom events are as follows:

1. inherit from the eventargs class to implement Custom Event parameters;

2. Define a delegate;

3. Define an event

4. Add events.

Next, let's take a look at the implementation of a custom event in the demo. This demo is a simple event monitoring call process for the status of network requests:

Custom Event parameter class

Statechangedeventargs. CS

 Using System;

Namespace Eventdemo
{
/// <Summary>
/// Status event
/// </Summary>
Public Class Statechangedeventargs: eventargs
{
Public Readonly String Newstate;
Public Readonly Datetime timestamp;

Public Statechangedeventargs ( String Newstate)
{
This . Newstate = newstate;
This . Timestamp = datetime. now;
}
}
}

Define events in the business class:

Nettask. CS

 Using System;
Using System. net;
Using System. Threading;
Using System. IO;

Namespace Eventdemo
{
Public Class Nettask
{
// Define Delegation
Public Delegate Void Statechanged (nettask sender, statechangedeventargs ARGs );
// Define events
Public Event Statechanged onstatechanged;
// Incident Status
Public String Nettaskname = "" ;

/// <Summary>
/// Network task
/// </Summary>
/// <Param name = "url"> </param>
Public Void Startnettask ( String URL)
{
Bool Success = False ;
Int Attempt = 0 ;
While (Attempt < 3 )
{
Asynccallback callback = Null ;
// Enable thread wait
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 ( " Retry " ));
Webrequestwait. Set ();
}
};
}
Request. begingetrequeststream (callback, request );

// Wait until the thread ends
Webrequestwait. waitone ();
If (SUCCESS)
{
Break ;
}
Attempt ++;
Thread. Sleep ( 1000 );
}
If (SUCCESS)
{
Onstatechanged ( This , New Statechangedeventargs ( " Successful " ));
Thread. Sleep ( 50 );
}
Else
{
Onstatechanged ( This , New Statechangedeventargs (" Failed " ));
}
}
}
}

A simple test

         <  Grid  X: Name  = "Contentpanel"  Grid. Row  = "1"  Margin  = "12, 0, 12, 0"  > 
< Button Content = "Test network" Height = "72" Horizontalalignment = "Left" Margin = "143,105" Name = "Button1" Verticalalignment = "TOP" Width = "202" Click = "Button#click" />
< Textblock Height = "50" Horizontalalignment = "Left" Margin = "96,270" Name = "Textblock1" Text = "Network status :" Verticalalignment = "TOP" Width = "126" />
< Textblock Height = "48" Horizontalalignment = "Left" Margin = "34,326" 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 Button#click ( Object Sender, routedeventargs E)
{
Nettask = New Nettask ();
Nettask. onstatechanged + = onstatechanged;
Nettask. nettaskname =" Test Network " ;
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 ();
}
}
}

The running result is as follows:

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.