开发者

Not able to use webClient.DownloadStringCompleted in WP7?

开发者 https://www.devze.com 2023-02-04 18:06 出处:网络
I have a simple code: using System; using System.Collections.Generic; using System.Linq; using System.Net;

I have a simple code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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;
using System.IO;

public partial class WebClient : PhoneApplicationPage
{
    public WebClient()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WebClient webclient = new WebClient();

Now when I say webClient., I expected to see DownloadStringCompleted in the IntelliSense dropdown but I dont see. And when I fo开发者_如何学Gorce use it, ofcourse it doesn't compile. What is the problem?

I am testing WebClient to see if it is of use in my project since I am fed up with the async calls and multiple threads associated with HttpWebRequest


You have for some strange reason used the name "WebClient" as the class name for your PhoneApplicationPage. Hence when you use this line:-

 WebClient webclient = new WebClient();

It attempts to create another instance of your page which of course does not have a DownloadStringCompleted or anything else provided by the WebClient in the System.Net namespace.

I would strongly suggest you give your page a different name. If you really did want to call your page "WebClient" then how about "WebClientPage"?


Seems to work ok here.

I opened the projected posted here.

WebClient, HttpWebRequest and the UI Thread on Windows Phone 7

Changed the WebClient usage to this

    webClient.DownloadStringAsync(new Uri("http://www.bing.com"));
    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);

and wrote the new event handler as

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
    System.Diagnostics.Debug.WriteLine("e.Error: " + e.Error);
    webClientTextBlock.Text = e.Result;
}

Worked just the same as before except I'm pulling a string down through e.Result instead of a stream.


You need to see the referrenced assemblies. Add the assemblies related to WP7 to see intellisense working properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消