开发者

WebRequest.Create (MSDN example)

开发者 https://www.devze.com 2023-02-20 09:22 出处:网络
I\'m trying to create a simple webrequest for a json, i\'m attempting to use the example on MSDN. // Create a new \'Uri\' object with the specified string.

I'm trying to create a simple webrequest for a json, i'm attempting to use the example on 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();

I'm getting the following error;

A field initializer cannot reference the non-static field, method, or property

on the objects highlighted. (myUri and开发者_运维技巧 myWebRequest) Any idea's?

thanks


This will not work because everything in Silverlight must be Async. They force this because all execution on the main thread like a webrequest would lock the UI. This approach provides a better user experience and is a trade-off to having developers master the use of threads for basic development activities.

See this:

How to use HttpWebRequest (.NET) asynchronously?

0

精彩评论

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