Bri*_*n D 1 c# silverlight windows-phone-7
我正在尝试学习C#/ Silverlight/Windows Phone 7.这里发生了什么:当我尝试直接使用MS的MSDN站点上的示例时,我遇到了各种错误:
例如:
using System;
using System.IO;
using System.Collections.Generic;
using System.Device.Location;
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 Microsoft.Phone.Tasks;
using System.Device.Location;
using Microsoft.Phone.Reactive;
private void registerPhone(object sender, RoutedEventArgs e)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
Run Code Online (Sandbox Code Playgroud)
它告诉我
1) The name 'Encoding' does not exist in this context
2) System.Net.WebRequest does not contain a definition for GetStreamRequest and no extension method GetRequestStream accepting a first argument of type System.Net.WebRequest could be found (are you missing a using directive... etc
3)ContentLength的类似消息
4)GetResponse的类似消息
我不知道我需要什么库"使用",即使我认为我"正在使用"正确的库,它也会给我带来错误.我究竟做错了什么?
本Encoding类驻留在System.Text命名空间.你必须将它添加到"使用".
至于其他消息,这与Silverlight的局限性有关.我认为,MSDN样本取自.NET Framework的"大"版本,不是吗?
在Silverlight中,您会看到,为了防止不良应用程序行为(例如阻止调用),某些内容是不允许的,并且由于资源限制,某些内容不受支持.
特别ContentLength是后者之一.库将根据您写入请求的实际数据量确定内容长度.只需删除该行,您就可以了.
但GetResponseStream()实际上是前者之一.这与此操作意味着实际打开网络连接这一事实有关,这可能需要一段时间.由于Silverlight中不允许阻塞调用,因此该GetResponseStream()方法也必须使用.
相反,你应该使用所谓的"异步模式" - 即一对方法BeginGetResponseStream/ EndGetResponseStream.您可以通过调用Begin方法并提供将在操作完成后调用的回调来执行此操作.然后,在该回调中,您使用该End方法来获取操作的结果.像这样:
request.BeginGetRequestStream( ar =>
{
var dataStream = request.EndGetRequestStream( ar );
// Write the data to the request stream.
dataStream.Write( byteArray, 0, byteArray.Length );
// Close the Stream object.
dataStream.Close();
// and so on...
}, null );
Run Code Online (Sandbox Code Playgroud)
该GetResponse方法也是如此.