WP7-网络-读取网页源码
生活随笔
收集整理的這篇文章主要介紹了
WP7-网络-读取网页源码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題:如何讀取一個網頁的源碼
我的理解:其實很簡單,只需要用“WebClient”類即可
?
示例:
讀取百度首頁源碼,然后放到messgaebox中show出來
?
前置條件:
1) C#基礎
2) XAML基礎
?
實現:
目錄:
1) 創建一個基本應用
2) 添加一個按鈕
3) 添加事件代碼
4) 測試運行
?
1 創建一個基本應用
1) 創建一個基本的Windows Phone應用程序,OS版本7.0
2) 清除多余的界面元素(留下一個基本的Grid控件)
1 <Grid x:Name="LayoutRoot" Background="Transparent"> 2 3 </Grid>2 添加一個按鈕
1 <Grid x:Name="LayoutRoot" Background="Transparent"> 2 <Button Content="Do" Height="72" HorizontalAlignment="Left" Margin="157,169,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" /> 3 </Grid>3 添加事件代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using Microsoft.Phone.Controls; 13 using System.IO; 14 15 namespace PhoneApp1 16 { 17 public partial class MainPage : PhoneApplicationPage 18 { 19 // 構造函數 20 public MainPage() 21 { 22 InitializeComponent(); 23 24 } 25 26 private void button1_Click(object sender, RoutedEventArgs e) 27 { 28 WebClient webClient = new WebClient(); 29 webClient.OpenReadAsync(new Uri("http://www.baidu.com")); 30 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 31 } 32 33 void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 34 { 35 using (StreamReader reader = new StreamReader(e.Result)) 36 { 37 MessageBox.Show(reader.ReadToEnd()); 38 } 39 } 40 } 41 }4 測試運行
?
?說明:
1) 使用OpenReadAsync方法設置需要讀取的頁面,其本意是“開啟指定資源的數據流”
2) 設置OpenReadCompleted事件,并在事件中讀取結果
?
轉載于:https://www.cnblogs.com/cation/archive/2012/11/02/2751515.html
總結
以上是生活随笔為你收集整理的WP7-网络-读取网页源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven学习十之myEclipse搭建
- 下一篇: 库克的时代?