0

Below I have a line of code that calls SearchAlbums(text). By removing this line I no longer get StackOverflowException so I believe this is the line that gives me the trouble.

    public ObservableCollection<AlbumView> Albums = new ObservableCollection<AlbumView>();

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (e.Parameter is string text)
        {
            // User Search
            MainPage.Instance.SetHeaderText(GetSearchHeader(text, MainPage.Instance.IsMinimal));
            History.Push(text);
            SearchArtists(text);
            SearchAlbums(text);
            SearchSongs(text);
            SearchPlaylists(text);
        }
        else
        {
            // Back to Search Page
            MainPage.Instance.SetHeaderText(GetSearchHeader(History.Pop(), MainPage.Instance.IsMinimal));
        }
    }

    public void SearchAlbums(string text)
    {
        Albums.Clear();
        foreach (var group in MusicLibraryPage.AllSongs.Where((m) => IsTargetAlbum(m, text)).GroupBy((m) => m.Album))
        {
            Music music = group.ElementAt(0);
            Albums.Add(new AlbumView(music.Album, music.Artist, group.OrderBy((m) => m.Name).ThenBy((m) => m.Artist)));
        }
    }

Therefore, I set a breakpoint in this function and I was actually able to run this function without an exception. The StackOverflowException occurred after OnNavigatedTo has been executed.

I think the constructor of AlbumView might have something to do with it:

    public AlbumView(string name, string artist, IEnumerable<Music> songs)
    {
        Name = name;
        Artist = artist;
        Songs = new ObservableCollection<Music>(songs);
        FindThumbnail();
    }
    public async void FindThumbnail()
    {
        foreach (var music in Songs)
            if ((Cover = await Helper.GetThumbnailAsync(music, false)) != null)
                break;
        if (Cover == null) Cover = Helper.DefaultAlbumCover;
    }

But I also use this constructor elsewhere in another page, and that page displayed perfectly.

The first piece of code posted above is in this page. And the exception occurs when there is a match album.

I don't know how to fix it. I can only guess it might be the issue of async operation. Thanks in advance!

8
  • 1
    StackOverflowException should contain a stacktrace which will explain the call tree. I suspect that Albums.Add somehow indirectly leads to invocation of OnNavigatedTo Commented Oct 9, 2019 at 1:31
  • @fenixil I am also hoping that VS could show me the stacktrace but I wasn't able to find it. It just displays a new page that says StackOverflowException. Do you know where I can find it? Albums is just a ObservableCollection<AlbumView>. Commented Oct 9, 2019 at 1:33
  • There are a lot of useful answers in stackoverflow, just a little of googling and you'll get what you need. Please try this answer. Or just put a breakpoint, and step through your code. Commented Oct 9, 2019 at 2:53
  • @fenixil I have tried breakpoint as explained in my question but it doesn't seem helpful. Thanks for the stacktrace. I will try it later. Commented Oct 9, 2019 at 3:10
  • @fenixil That answer doesn't work for me and the call stack is not showing any functions. OnNavigatedTo is only called once. Commented Oct 9, 2019 at 4:19

1 Answer 1

1

I tested your code. Your problem is not in code-behind, but on the control.

For instances that have an asynchronous, use Binding instead of x:Bind for the binding of the instance. Because Binding is a runtime binding, and x:Bind is a compile-time binding.

change your code to this:

SearchPage.xaml

...
<controls:Carousel.ItemTemplate>
    <DataTemplate x:DataType="data:AlbumView">
        <local:GridAlbumControl DataContext="{Binding}" />
    </DataTemplate>
</controls:Carousel.ItemTemplate>
...

Best regards.

Sign up to request clarification or add additional context in comments.

6 Comments

Excuse me, I just found another StackOverflowException, could you please test it? It occurs when I enter the SearchPage and then close the app. My code has also been updated.
Sorry, I can't reproduce this question. If the StackOverflowException is displayed as before, please try to comment out some of the controls that may be reported in xaml and then locate the cause of the problem.
Maybe because you didn't get any search result or you are not using my latest code? In my test, I found out that as long as there is any search result for any of the Artists/Albums/Songs/Playlists, StackOverflowException would occur on exit when you are on the SearchPage. I tried commenting out every control of the 4 StackPanels that holds those data but it still occurred. So it doesn't seem to be an XAML issue?
@SeakyLone please make sure you learnt how to troubleshoot StackOverflowException so that you don't need to post a question on each occurrence ;)
@fenixil I don't think this is a c# related question just as this answer suggests. My call stack doesn't have anything c# related. I just checked that my debugging settings by writing a recursive function. It correctly gives me the callstack with a bunch of that function. But my problem is it doesn't report a c# problem. So I don't know what else I can look at.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.