UWP System.StackOverflowException

UWP System.StackOverflowException

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

这是我的代码`

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();
    }`

每次调用一个方法,都会增加堆栈的使用。当SelectedIndex == 2时,你的方法只会一遍又一遍地调用自己,导致堆栈溢出。

could you teach how to exit out ?

使用return语句。

附带说明一下,您应该使用 async Taskasync void主要用于事件处理程序。