2012-01-22 5 views
1

私はWP7用のC#で開発するのが非常に新しいです。私は、テキストボックス1からurlを受け取り、button1が押されたときに、そのページのソースコードでtextBlock1のテキストを更新する簡単なアプリケーションを作成しようとしています。Windows PhoneアプリケーションのWebページソースコードを掻き集める

私が取り組んでいるのは、DownloadStringCallback2のResultをLoadSiteContent関数に渡して変数sourceCodeとして返す方法です。次のように

コードは次のとおりです。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 

namespace TestApp1 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      string url = textBox1.Text; 
      string sourceCode = LoadSiteContent(url); 
      textBlock1.Text = sourceCode; 
     } 

     /// <summary> 
     /// method for retrieving information from a specified URL 
     /// </summary> 
     /// <param name="url">url to retrieve data from</param> 
     /// <returns>source code of URL</returns> 
     public string LoadSiteContent(string url) 
     { 
      //create a new WebClient object 
      WebClient client = new WebClient(); 

      //create a byte array for holding the returned data 
      string sourceCode = "Fail"; 
      client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2); 
      client.DownloadStringAsync(new Uri(url)); 

      //use the UTF8Encoding object to convert the byte 
      //array into a string 
      //UTF8Encoding utf = new UTF8Encoding(); 

      //return the converted string 
      //return utf.GetString(html, 0, html.Length); 
      return sourceCode; 
     } 

     private static void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e) 
     { 
      // If the request was not canceled and did not throw 
      // an exception, display the resource. 
      if (!e.Cancelled && e.Error == null) 
      { 
       string textString = (string)e.Result; 
      } 
     } 
    } 
} 

答えて

2

WP7(シルバー)のAlのWeb要求が非同期であるので、あなたは、したいと思いますようにあなたがそれを行うことはできません。 これは、Webページをダウンロード中にコードが停止せず、同じ行と関数で終了したときにコードが停止して新しいスレッドを作成し、ファイルをダウンロードしてコールバック関数を呼び出すことを意味します。

コールバック関数(あなたのケースではDownloadStringCallback2)を続行する必要があります。 この関数では、ソースコード(e.Result)をテキストボックスに配置する必要があります。あなたがクロススレッドの例外を取得する場合

は、私はそれに追加したり、タスクを実行しながら、ちゃんと使えるUIを維持したい場合は、このコマンドを使用することができます。

Dispatcher.BeginInvoke(new Action (() => LoadContent("http://www.google.com"))); 

このコマンドは、十字架を修正しますスレッド例外(私が正しく覚えている場合)とUIスレッドとは別のスレッドでコードを実行し、安定したUIを維持します。

EDIT私はあなたのコードは次のようになりますと思う: `プライベートの静的な無効DownloadStringCallback2(オブジェクト送信者、DownloadStringCompletedEventArgs E) { //:私は今のように見えるようにコールバック関数を更新

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     string url = textBox1.Text; 
     LoadSiteContent(url); 
    } 

    public string LoadSiteContent(string url) 
    { 
     //create a new WebClient object 
     WebClient client = new WebClient(); 

     client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2); 
     client.DownloadStringAsync(new Uri(url)); 
    } 

    private static void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e) 
    { 
     // If the request was not canceled and did not throw 
     // an exception, display the resource. 
     if (!e.Cancelled && e.Error == null) 
     { 
      textBlock1.Text = (string)e.Result; 
      //If you get the cross-thread exception then use the following line instead of the above 
      //Dispatcher.BeginInvoke(new Action (() => textBlock1.Text = (string)e.Result)); 
     } 
    } 
+0

要求がキャンセルされず、 //例外をスローしなかった場合は、リソースを表示します。 if(!e.Cancelled && e.Error == null) { textBlock1.Text =(文字列)e.Result; }} ' 私はエラーを取得しています :オブジェクト参照が非静的フィールド、メソッド、またはプロパティのために必要とされる「TestApp1.MainPage.textBlock1」 あなたは私が間違っているの何任意のアイデアを持っていますか? –

+0

メインページにあるtextBlock1を編集しようとしていることと、コールバック関数が静的であるためtextBlock1が認識されないためです。これに対処する私の方法は代理人を使用していました。代理人は非同期イベントのためにwp7で本当に便利です。私はあなたに知られていない場合はそれらを検索することをお勧めします。 'public static MainPage mainPage;を' mainPage = this; 'のようにコンストラクタに入れて、' mainPage.textBlock1.Text =(string)e.Result;でtextBlock1を塗りつぶすこともできます。 。より簡単な方法があるかもしれませんが、私はatmをテストすることはできません。 –

+0

私はちょうど私がこの記事に追いついたことがないことに気づいた。あなたのアドバイスはとても役に立ちました。私はあなたの答えを投票しますが、私は新しいユーザーです。回答いただきありがとうございます。 –

関連する問題