Windows Phone 8 ProgressBar与ListBox错误显示

时间:2016-03-28 15:49:22

标签: c# xaml windows-phone-8

我的app有这样的情况,当执行OnNavigatedTo时,ProgressBar会正确显示。然后在搜索和整理重新生成的数据的过程中,ProgressBar挂起/冻结。最后,当所有数据都已在列表中正确收集并显示时,ProgressBar会正确折叠。

我的XAML看起来像这样:

<ProgressBar
    x:Name="progressbar"
    IsIndeterminate="True"
    Minimum="0"
    Maximum="100"
    Value="0"
    Width="400"
    Height="30"
    Foreground="#FF117B0F"
/>

我的C#看起来像这样:

List<GameLive> gameLive;

public MainPage()
{
    InitializeComponent();
    gameLive = new List<GameLive>();
    ProgressBar progressbar = new ProgressBar();
}

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    string htmlPageLive = "";
    bool isError = false;

    HttpClient client = new HttpClient();

    try
    {
        htmlPageLive = await client.GetStringAsync("webpage");
    }

    catch (Exception)
    {
        isError = true;
    }

    if (isError)
    {
        MessageBox.Show("Unable to retrieve data");
        return;
    }

    HtmlDocument htmlDocumentLive = new HtmlDocument();
    htmlDocumentLive.LoadHtml(htmlPageLive);

    foreach (var div in htmlDocumentLive.DocumentNode.SelectNodes("..nav.."))
    {
        bool isErrorTwo = false;
        GameLive newGame = new GameLive();
        try
        {
            newGame.Title = removed;
            newGame.Summary = removed;

            gameLive.Add(newGame);
        }

        catch (Exception)
        {
            isErrorTwo = true;
        }

        if (isErrorTwo)
        {
            NavigationService.Navigate(new Uri("/Temp.xaml", UriKind.Relative));
            return;
        }
    }
        lstGameLive.ItemsSource = gameLive;
        progressbar.Visibility = Visibility.Collapsed;
        progressbar.IsIndeterminate = false;
}

我尝试了多种解决方案,但已经没有选择。有人可以指点我在正确的方向吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

这是一个与异步方法相关的问题,因为它们会冻结UI(我认为)直到执行完成。

您可能希望查看this以及this

要获得更清晰的代码,请尝试绑定进度条可见性,例如this onethis