I am building an app in Xamarin.Forms that display news from different sources. I fetch the data from the internet using JSON Format. I am using the code below. What I want is to display some progress dialog during the time data are being fetched but I cannot place them inside the populate method.
How can it be possible. I am mainly interested for iOS project.
public partial class SearchNews : ContentPage
{
public static ObservableCollection<NewsClusterItem> newsItems { get; set; }
public static SearchResponseObject respObj;
public string base_url = "http://time.ikub.al/services/servicesNew/SearchNewsNew.ashx?";
public string news_cluster_per_page = "newsClustersPerPage=15&";
public string news_page_number = "pageNumber=";
public string query_string = "&query=";
public int page_number = 1;
public static string search_url;
public SearchNews (String text)
{
InitializeComponent ();
NavigationPage.SetBackButtonTitle (this, "");
this.Title = "Rezultatet për: " + text;
//forming the url
search_url = base_url + news_cluster_per_page + news_page_number + page_number + query_string + text;
newsItems = new ObservableCollection<NewsClusterItem> ();
listView.ItemsSource = newsItems;
populate ();
}
async static void populate(){
try
{
string contents;
string Url = String.Format(search_url);
HttpClient hc = new HttpClient();
contents = await hc.GetStringAsync(Url);
respObj = JsonConvert.DeserializeObject<SearchResponseObject>(contents);
if(respObj.moreNews){
for(int i =0; i<respObj.news.Count;i++){
newsItems.Add(respObj.news[i]);
}
}
}
catch (System.Exception sysExc)
{
// Do something to log this error.
throw;
}
}