C# Windows 8 中不存在 WebClient 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9482402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebClient class doesn't exist in Windows 8
提问by NicoMinsk
I want to use a HTTP webservice, and I've already developed an app for wp7.
我想使用 HTTP 网络服务,并且我已经为 wp7 开发了一个应用程序。
I use the WebClient class, but I can not use it for windows 8 ("error: type or namespace can not be found").
我使用 WebClient 类,但我不能将它用于 Windows 8(“错误:找不到类型或命名空间”)。
What else can I use?
我还能用什么?
Can you provide me a sample of code?
你能给我一个代码示例吗?
Does Microsoft have a site to help when a namespace don't exist?
当命名空间不存在时,Microsoft 是否有一个站点可以提供帮助?
采纳答案by sarvesh
Option 1 : HttpClient if you don't need deterministic progress notification this is what you want use. Example.
选项 1 : HttpClient 如果您不需要确定性进度通知,这就是您想要使用的。例子。
public async Task<string> MakeWebRequest()
{
HttpClient http = new System.Net.Http.HttpClient();
HttpResponseMessage response = await http.GetAsync("http://www.example.com");
return await response.Content.ReadAsStringAsync();
}
Option 2: When you need progress notifications you can use DownloadOperationor BackgroundDownloader. This sampleon MSDN is a good start.
选项 2:当您需要进度通知时,您可以使用DownloadOperation或BackgroundDownloader。MSDN 上的这个示例是一个好的开始。
Option 3: Since you mentioned web service and if it is returning XML you can use XmlDocument.LoadFromUriAsyncwhich will return you an XML document. Example
选项 3:由于您提到了 Web 服务,并且如果它返回 XML,您可以使用XmlDocument.LoadFromUriAsync它将返回一个 XML 文档。例子
public async void DownloadXMLDocument()
{
Uri uri = new Uri("http://example.com/sample.xml");
XmlDocument xmlDocument = await XmlDocument.LoadFromUriAsync(uri);
//do something with the xmlDocument.
}
When you are developing for metro .Net framework will be limited compared to the desktop version. If you see namespace not found error it is usually due to this fact. This linkon the MSDN has the list of namespaces, classes available for metro.
当您为 Metro 开发时,与桌面版本相比,.Net 框架将受到限制。如果您看到未找到命名空间错误,通常是由于这个事实。MSDN 上的此链接包含可用于 Metro 的命名空间和类的列表。

