How to check HtmlWeb.LoadAsync finished

Topics: Developer Forum
Apr 17, 2012 at 6:18 AM
Edited Apr 17, 2012 at 6:20 AM

I have a class in Viewmodel folder that using HtmlWeb.LoadAsync to get data from web:

 

public void GetContent(int index)
        {
            //get content
            HtmlWeb.LoadAsync(Magazines[index].Url, (s, args) =>
            {

              ....

             this.Magazines[index].ContentNode = contentNode.InnerHtml;
            });

}

 

Then I want to get the Magazines[index].contentNode in detailview.xaml like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string selectedIndex = "";
           
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
              index = int.Parse(selectedIndex);
              App.MagazineViewModel.GetContent(index);
              String content = App.MagazineViewModel.Magazines[index].ContentNode;
              DetailBrser.NavigateToString(
                 "<html><head><meta name='viewport' content='width=570, user-scalable=yes' /></head><body>"
                 + HtmlHelper.EncodeUnicode(content)
                 + "</body></html>"
                 );
            }

But the problem is the loadAsync method has not finished yet, so App.MagazineViewModel.Magazines[index].contentNode is empty. that also make content empty.  so how can I check App.MagazineViewModel.GetContent(index) finish in detailview.xaml then set the content string. Or any other idea for this.

May 11, 2012 at 4:23 PM

Not sure if you thought of this, but why don't you just put all of the code that depends on the loaded content into the method that gets called when LoadAsync finishes? So everything after GetContent(index) in OnNavigatedTo() gets moved into LoadAsync() after

this.Magazines[index].ContentNode = contentNode.InnerHtml;