Windows Phone Universal App错误在加载时调用异步函数

时间:2014-09-21 13:14:17

标签: c# windows-phone-8 asynchronous async-await win-universal-app

嗨我在windows phone主窗口代码上有这段代码

namespace Balcão_Virutal_UBI
{

    public sealed partial class MainPage : Page
    {
        public bool hasloggin = false;
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

            fetchInfo();
        }

        private async void fetchInfo()
        {
            FetchData fd = new FetchData();
            Dictionary<string, string> schedule = await fd.GetSchedule_HC("12", "ui");
            foreach (KeyValuePair<string, string> entry in schedule)
            {
                horario.Items.Add(entry.Key + "|" + entry.Value);
            }
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }
    }
}

和被调用的函数

HtmlWeb htmlW;

    public async Task<Dictionary<string, string>> GetSchedule_HC(string day, string week)
    {
        string year = "21"; //get from settings
        string semestre = "S1"; //get from settings
        string ciclo = "1"; //get from settings
        string course = "1408"; //get from settings
        string url = "https://academicos.ubi.pt/online/horarios.aspx?p=a";
        string cicleY = "1"; //get from settings

        htmlW = new HtmlWeb();
        HtmlDocument htmlDoc = await htmlW.LoadFromWebAsync(url);
        IEnumerable<HtmlNode> selectElm = from selectNode in htmlDoc.DocumentNode.Descendants()
                                          where selectNode.Name == "select" && selectNode.Attributes["id"].Value == "ContentPlaceHolder1_ddlAnoLect"
                                          select selectNode;
        Dictionary<string, string> rtnval = new Dictionary<string,string>();
        foreach (HtmlNode node in selectElm)
        {
            rtnval.Add(node.Attributes["value"].Value, node.InnerText);
        }
        return rtnval;
    }

我在启动app global :: System.Diagnostics.Debugger.Break();

时遇到此错误

我有等待的消息框并切换到列表框,但问题是一样的...我试图找到解决方案,但我无法

2 个答案:

答案 0 :(得分:0)

在触发Page Loaded事件之前,不会构建可视树。尝试将fetchInfo()函数移动到Loaded事件中。

答案 1 :(得分:0)

使用断点和StepByStep调试进行mutch测试后,我能够找出问题... 感谢Jeff Sanders,这个提示在加载内容之前帮助UI完全加载,其余的解决方案是由葡萄牙的一个用户在Programar论坛中特别给我的。 这是修复

public async Task<Dictionary<string,string>> GetSchedule_HC(string day, string week)
        {
            string year = "21"; //get from settings
            string semestre = "S1"; //get from settings
            string ciclo = "1"; //get from settings
            string course = "1408"; //get from settings
            string url = "https://academicos.ubi.pt/online/horarios.aspx?p=a";
            string cicleY = "1"; //get from settings


            string htmlPage;
            using(var wp = new HttpClient())
            {
                htmlPage = await wp.GetStringAsync(new Uri(url));
            }

            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(htmlPage);
            Dictionary<string, string> rtnval = new Dictionary<string, string>();
            HtmlNodeCollection selectNode = htmlDoc.GetElementbyId("ContentPlaceHolder1_ddlAnoLect").ChildNodes;

            foreach (HtmlNode optNode in selectNode)
            {                
                if (optNode.Name == "option")
                {
                    string txt = optNode.NextSibling.InnerText;
                    string val = optNode.Attributes["value"].Value;
                    rtnval.Add(val,txt);
                }
            }



            return rtnval;
        }