UWP System.StackOverflowException

时间:2017-07-19 09:18:59

标签: uwp unhandled-exception

我正在创建新闻源要测试它,我使用combobox_selectionChanged事件来激活此功能,当selectedItem = 0显示世界标题时,selectedItem = 1显示体育标题,selectedItem = 2将可见性更改为折叠。当你选择item0或更改为item1时它可以工作,但是一旦我改为item2,它就会显示 system.StackOverflowException

这是我的代码`

CancellationTokenSource cts = null;
    public async void NewsRepeat()
    {          
        cts?.Cancel();
        try
        {
            var localCts = cts = new CancellationTokenSource();                           
            localCts.Token.ThrowIfCancellationRequested();
            if (newsTpye.SelectedIndex == 0)
            {
                NewsPanel.Visibility = Visibility.Visible;
                RootObject2 myNews = await NewsProxy.GetNews();
                newsChannel.Text = "World Headlines";
                for (k = 0; k <= 8; k++)
                {
                    if (myNews.articles[k].title != null)
                        showTitle.Text = myNews.articles[k].title;
                    else
                        showTitle.Text = "";
                    if (myNews.articles[k].urlToImage != null)
                        newsImage.Source = new BitmapImage(new Uri(myNews.articles[k].urlToImage, UriKind.Absolute));
                    else
                        newsImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/image/NoPic.jpg", UriKind.RelativeOrAbsolute));
                    if (myNews.articles[k].publishedAt != null)
                        showTime.Text = myNews.articles[k].publishedAt;
                    else
                        showTime.Text = "";
                    showDescription.Text = "(" + myNews.source + "): " + myNews.articles[k].description;
                    await Task.Delay(5000);
                    localCts.Token.ThrowIfCancellationRequested();
                }

            }
            else if (newsTpye.SelectedIndex == 1)
            {
                NewsPanel.Visibility = Visibility.Visible;
                RootObject3 mySportNews = await sportsNewsProxy.GetSportNews();
                newsChannel.Text = "Sports Headlines";

                for (k = 0; k <= 8; k++)
                {
                    if (mySportNews.articles[k].title != null)
                        showTitle.Text = mySportNews.articles[k].title;
                    else
                        showTitle.Text = "";

                    if (mySportNews.articles[k].urlToImage != null)
                        newsImage.Source = new BitmapImage(new Uri(mySportNews.articles[k].urlToImage, UriKind.Absolute));
                    else
                        newsImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/image/NoPic.jpg", UriKind.RelativeOrAbsolute));

                    if (mySportNews.articles[k].publishedAt != null)
                        showTime.Text = mySportNews.articles[k].publishedAt;
                    else
                        showTime.Text = "";
                    showDescription.Text = "(" + mySportNews.source + "): " + mySportNews.articles[k].description;
                    await Task.Delay(5000);
                    localCts.Token.ThrowIfCancellationRequested();
                }
            }
            else if (newsTpye.SelectedIndex == 2)
            {
                NewsPanel.Visibility = Visibility.Collapsed;
            }
            NewsRepeat();
        }
        catch (OperationCanceledException)
        {
            // Swallow this exception only - this is probably
            // the one we've thrown ourselves
        } 
    }

    private void newsType_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        NewsRepeat();
    }`

enter image description here

1 个答案:

答案 0 :(得分:2)

每次调用方法时,都会增加堆栈使用量。当SelectedIndex == 2时,您的方法将一遍又一遍地调用自身,导致堆栈溢出。

  

你能教导如何退出吗?

使用return声明。

另外,您应该使用async Taskasync void主要用于事件处理程序。