自定义事件和windows phone访问网站取得数据

    昨天看了一位园友的自定义事件的文章,我以前学习过这方面的内容,但在实际工作中基本没用过自定义事件,前天就边看着,自己写了写代码把这个知识点复习了。下面上代码

  事件类代码

public class EvevtClass:EventArgs
{
public string State;
public string Data;
public DateTime Time;
public EvevtClass(string state, string data)
{
this.Time = DateTime.Now;
this.Data = data;
this.State = state;
}
}

这上面的代码我是看一位园友写的,但是这其中我有个问题。就是我写的这个类非要继承EventArgs类吗?但是我试了下,如果不继承程序运行效果是一样的。我记得我当时学习事件的时候说是自定义事件类必须继承EventArgs类,如果我不继承呢?还是我记错了。

下面调用事件类

public class NetTask
{
public delegate void Dele(NetTask sender, EvevtClass args);
public delegate void DeleTwo(object o, string str);
public event DeleTwo Change;
public event Dele OnStateChanged;
public string Data;
public void StartNetTask(string url)
{
bool success = false;
//int attempt = 0;
//while (attempt < 3)
//{
// AsyncCallback callBack = null;
// ManualResetEvent webRequestWait = new ManualResetEvent(false);
// WebRequest request = WebRequest.Create(url);
// if (callBack==null)
// {
// callBack = (ar) =>
// {
// try
// {
// WebRequest re = ar.AsyncState as WebRequest;
// WebResponse response = re.EndGetResponse(ar);
// Stream stream = response.GetResponseStream();
// StreamReader reader = new StreamReader(stream);
// Data = reader.ReadToEnd();
// success = true;
// webRequestWait.Set();
// }
// catch (Exception)
// {

// }

// };
// }
// request.BeginGetResponse(callBack, request);
////Thread.Sleep(9000);
// webRequestWait.WaitOne();
// if (success)
// {
// break;
// }
// attempt++;
// Thread.Sleep(1000);
//}
WebRequest request = WebRequest.Create(url);
AsyncCallback callBack = (ar) =>
{
try
{
WebRequest re = ar.AsyncState as WebRequest;
WebResponse response = re.EndGetResponse(ar);
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
Data = reader.ReadToEnd();
success = true;
}
catch (Exception)
{

}
if (success)
{
OnStateChanged(this, new EvevtClass("网络连接成功", Data));
}
else
{
OnStateChanged(this, new EvevtClass("网络连接失败", "没数据"));
}

};
request.BeginGetResponse(callBack, request);
//Thread.Sleep(9000);


}
}

下面是ViewModel类

public class ViewModel:INotifyPropertyChanged
{
string state;
public string State
{
set
{
state = value;
Raise("State");
}
get
{
return state;
}

}
string data;
public string Data
{
set
{
data = value;
Raise("Data");
}
get
{
return data;
}
}
ICommand buttomCommand;
public ICommand Button
{
get
{
buttomCommand = new DeleCommand((o) =>
{
NetTask net = new NetTask();
net.OnStateChanged += new NetTask.Dele(net_OnStateChanged);
net.OnStateChanged += new NetTask.Dele(Method);///这行多此一举,是为了加深事件的理解而写
net.StartNetTask(o);
//State = "state";
//Data = "datadatadtaa";
}
);
return buttomCommand;
}
}
void net_OnStateChanged(NetTask sender, EvevtClass args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
State = args.State + args.Time;
}
);
}
void Method(NetTask sender, EvevtClass args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Data = args.Data;
}
);
}
public event PropertyChangedEventHandler PropertyChanged;
void Raise(string name)
{
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}

实现了ICommand类

public class DeleCommand:ICommand
{
public DeleCommand(Action<string> a)
{
Del = a;
}
public Action<string> Del;
public bool CanExecute(object parameter)
{
return true;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
Del(parameter.ToString());
}
}

界面

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="测试连接" CommandParameter="{Binding ElementName=textbox,Path=Text}" Command="{Binding Button}" Click="Button_Click"></Button>
<StackPanel Grid.Row="1">
<TextBox Name="textbox" Text="http://www.cnblogs.com"></TextBox>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="28">状态:</TextBlock>
<TextBlock Name="state" Text="{Binding State}"></TextBlock>
</StackPanel>
<StackPanel >
<TextBlock FontSize="28"> 数据:</TextBlock>
<ScrollViewer Height="500">
<TextBlock Text="{Binding Data}" TextWrapping="Wrap" Name="data"></TextBlock>
</ScrollViewer>
</StackPanel>
</StackPanel>
</Grid>

最后运行效果图

OK完了。希望有明白的人,回答下我的那个疑问!

posted @ 2012-04-05 12:30  learnWindowsPhone  阅读(165)  评论(0编辑  收藏  举报