WebRequest.Create(MSDN示例)

时间:2011-03-29 17:01:32

标签: c# silverlight

我正在尝试为json创建一个简单的webrequest,我正在尝试在MSDN上使用该示例。

// Create a new 'Uri' object with the specified string.
        Uri myUri =new Uri("http://www.contoso.com");
        // Create a new request to the above mentioned URL. 
        WebRequest myWebRequest= WebRequest.Create(**myUri**);
        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse myWebResponse= **myWebRequest**.GetResponse();

我收到以下错误;

字段初始值设定项不能引用非静态字段,方法或属性

突出显示的对象。 (myUri和myWebRequest)有什么想法吗?

感谢

1 个答案:

答案 0 :(得分:2)

这不起作用,因为Silverlight中的所有内容都必须是异步。他们强迫这一点,因为主要线程上的所有执行都像webrequest一样会锁定UI。这种方法提供了更好的用户体验,并且是让开发人员掌握线程用于基本开发活动的权衡。

见:

How to use HttpWebRequest (.NET) asynchronously?