A
A
Andrey Kornev2014-03-31 12:36:21
.NET
Andrey Kornev, 2014-03-31 12:36:21

How to create a synchronous request in windows phone 8?

The bottom line is that the application loads data into JSON at startup and saves it to isolated storage. As it turned out in wp8 WebClient does not contain a synchronous request, only an asynchronous one.
It goes like this for me.
JsonObj JsonObj = new JsonObj();
The constructor checks whether there is a data string in json in the isolated storage, if there is none, then a request is made to the server and the data is stored in the isolated storage and deserialized. and then, according to the idea, we work with the data.

JsonObj JsonObj = new JsonObj();
MainNewsList.ItemsSource = JsonObj.MAIN_NEWS;
NewsList.ItemsSource = JsonObj.NEWS;

The problem is that the data loading in the JsonObj constructor is asynchronous.
And as a consequence in
MainNewsList.ItemsSource = JsonObj.MAIN_NEWS;
NewsList.ItemsSource = JsonObj.NEWS;

there is nothing, and the first time the application loads, I get a blank page.
How to make a synchronous http request?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
ad1Dima, 2014-03-31
@webkornevand

The answer to your question is: NO!
Explanation: DON'T DO IT!
Synchronous requests to the server were removed SPECIALLY, so that those who like to load data in the UI thread would not have such an opportunity.
Hang a progress bar for the loading time, and when the data is loaded, display them, there is nothing complicated about it. For syntactic sugar, you can also install Async CTP.

A
ad1Dima, 2014-04-01
@ad1Dima

Take an Action in the class constructor

public JsonObj(Action callback)
{
   //...
}

call this action at the end of loading callback.Invoke();
when instantiating, pass a lambda
JsonObj JsonObj = new JsonObj(()=>
{
MainNewsList.ItemsSource = JsonObj.MAIN_NEWS;
NewsList.ItemsSource = JsonObj.NEWS;
});

V
Vyacheslav Zolotov, 2014-04-01
@SZolotov

You can download synchronously, but why?
For HttpClient (there is a nuget of the same name) there will be something like this: var client = new HttpClient(); var result = client.GetStringAsync(/*your url*/).Result;

A
Andrey Kornev, 2014-04-01
@webkornevand

Found, installed.
the app is just in endless waiting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question