'Asynchronous'에 해당되는 글 3건
- 2009/07/13 Silverlight - Network Example 1
- 2009/04/30 Silverlight - Network (2)
- 2009/03/11 sunyruru의 미투데이 - 2009년 3월 11일
자료제공 : http://hugeflow.com/
3-1.WebClientTest.zip |
이전 내용에 Silverlight의 네트워크에 작성한 내용이 있다.
2009/04/30 - [.NET/SilverLight] - Silverlight - Network
이 내용을 바탕으로 다음의 소스코드를 작성하였으므로 내용의 이해가 조금 부족하면 확인하시길...
예제는 프로젝트명 WebClientTest로 하였고,
먼저 Page.xaml 코드는
<UserControl x:Class="WebClientTest.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="Auto" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="418">
<Grid x:Name="LayoutRoot" Background="White"><TextBox Height="47" Margin="8,8,100,0" VerticalAlignment="Top" Text="http://hugeflow.com/sample.xml" TextWrapping="Wrap" x:Name="tboxAddress"/>
<Button HorizontalAlignment="Right" Margin="0,8,8.192,0" VerticalAlignment="Top" Content="Get" Width="88" Height="47" x:Name="buttonGet" Click="buttonGet_Click"/>
<TextBlock Margin="8,59,8,8" Text="" TextWrapping="Wrap" x:Name="tbOutput"/></Grid>
</UserControl>
다음으로 C# 코드
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;namespace WebClientTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}private void buttonGet_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tboxAddress.Text) == true)
{
return;
}try
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(tboxAddress.Text, UriKind.Absolute));
}
catch (Exception ex)
{
tbOutput.Text = ex.Message;
throw;
}
}void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Cancelled == true)
{
tbOutput.Text = "취소되었습니다.";
return;
}if (e.Error != null)
{
if (string.IsNullOrEmpty(e.Error.Message) == false)
{
tbOutput.Text = e.Error.Message;
}
else
{
tbOutput.Text = e.Error.ToString();
}
return;
}tbOutput.Text = e.Result;
}
}
}
'.NET > Silverlight' 카테고리의 다른 글
| Silverlight + Expression = Visual Kitchen (0) | 2009/07/16 |
|---|---|
| Silverlight - Network Sample 2 (0) | 2009/07/15 |
| Silverlight - Network Example 1 (0) | 2009/07/13 |
| Silverlight – Custom control (0) | 2009/07/03 |
| Silverlight Custom control (0) | 2009/06/23 |
| Silverlight – bubbling events (2) | 2009/05/18 |
자료 출처 : http://hugeflow.com/
실버라이트 2.0에는 동기방식의 통신은 없애고 비동기 통신만 남겨두었다.
이 방식을 사용하기에 WebClient와 WebRequest 두가지를 사용할 수 있는데.
WebClient가 약간 더 코드가 간결하고 WebRequest는 어렵다. 하지만 크게 어려운 것은 아니다.
다음의 기본 사용 예로 설명을 대신하도록 한다.
WebClient
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("http://hugeflow.com/sample.xml", UriKind.RelativeOrAbsolute));
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
}
HttpWebRequest
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://hugeflow.com/sample.xml", UriKind.Absolute));
request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
}
}
'.NET > Silverlight' 카테고리의 다른 글
| Silverlight - Access HTML document (0) | 2009/05/07 |
|---|---|
| Silverlight - Access managed code form javascript (0) | 2009/05/05 |
| Silverlight - Network (2) | 2009/04/30 |
| Silverlight domain access control (0) | 2009/04/28 |
| REMIX08 RIA, UX의 세계 (0) | 2008/06/16 |
| Programming Silverlight with JavaScript (0) | 2008/05/28 |
- HTML for YES-SCRIPT 흠 그랬군요 NOSCRIPT 말고 반대도 가능하군요..2009-03-10 09:16:32
- Asynchronous Method Invocation이라는 글로 비동기 호출을 위해 Delegate를 사용하는 적절한 예가 있네요. 연구해서 제 개인 블로그로 포스팅 해야겟네요. 비동기 메소드 호출에 대해 관심있으시면 꼭 보세요~! 단 C#.NET입니다.2009-03-11 01:57:58
- C# Coding Standards and Best Programming Practices 간단히 말해 C# 코딩 스타일 가이드가 되겠습니다. 영문 버전으로 유명한 글인데 한글로 번역된 것이 있었네요. 알고 있는 내용이지만, 그래도 가끔씩은 봐줄만 합니다. ㅎㅎ2009-03-11 02:21:23
이 글은 sunyruru님의 2009년 3월 10일에서 2009년 3월 11일까지의 미투데이 내용입니다.
'FunFun' 카테고리의 다른 글
| sunyruru의 미투데이 - 2009년 3월 12일 (0) | 2009/03/13 |
|---|---|
| sunyruru의 미투데이 - 2009년 3월 11일 (0) | 2009/03/12 |
| sunyruru의 미투데이 - 2009년 3월 11일 (0) | 2009/03/11 |
| sunyruru의 미투데이 - 2009년 3월 5일 (0) | 2009/03/06 |
| sunyruru의 미투데이 - 2009년 2월 27일 (0) | 2009/02/28 |
| sunyruru의 미투데이 - 2009년 2월 20일 (0) | 2009/02/21 |

3-1.WebClientTest.zip
이올린에 북마크하기
이올린에 추천하기



Prev




