I have to preface this with a disclaimer. I'm a novice programmer, I've tried solving this on my own for days but have now completely run out of ideas/blog posts/walkthroughs and other sources. I really appreciate your time in reading and potentially replying.
I am trying to integrate scoreloop into a game I'm developing but am getting some very strange results with data binding and a listbox. My tests (below) imply that there has to be something I'm doing wrong with bindings, but the crazy thing is it actually works the first time I use it, but not for subsequent levels. Here is the important code I'm using:
XAML:
<ListBox x:Name="LeftListBox" Margin="12,48,0,128" ItemsSource="{Binding}" Background="{x:Null}" HorizontalAlignment="Left" Width="240">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<StackPanel Margin="0,0,0,0" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Text="{Binding Rank}" TextWrapping="NoWrap" />
<TextBlock Text="." Width="54"/>
<TextBlock Text="{Binding Result}" TextWrapping="NoWrap" Width="76"/>
<TextBlock Text="{Binding User.Login}" TextWrapping="NoWrap"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have an event registered to fire whenever scores are loaded, this sets the binding and logs a debug message:
LeftListBox.DataContext = App._scoresController.Scores;
Debug.WriteLine("Scores Loaded");
App._scoresController.Scores contains User.Login, Rank, and Result.
After I beat a level it pulls down scores and displays them int he listbox just like I expect. As soon as it goes through the same cycle for the next level though the listbox is blank. The debug line of "Scores Loaded" always gets logged, so I know the event is firing.
What I've done so far in testing:
- Turned on ALL for bindings logging and could not see anything getting logged in the output.
- Set a break point at the Debug "Scores Loaded" line and can see that everytime it hits there it correctly assigned the datacontext, with the correct fields with exactly matching names
- Tried using Dispatcher.BeginInvoke(LoadScores); to be sure I was doing it off the UI thread in case this was somehow a threading issue
- Set the background color on the stackpanel to a color that I could use to ensure it wasn't being collapsed or hidden by another control or a storyboard animation
- Created a copy of the same listbox, set listbox.datacontext = this in the same LoadScores() method, then set up local variables for it to bind to. Found that this exhibited the same behavior, disappearing on the second time I go to set the datacontext
- Created a copy of the listbox and removed all bindings, setting the three text fields manually. This would not disappear, but showed up every time I beat a level
- Beat one level (getting it to work), beat another (getting it to disappear), navigate away from the gamepage.xaml/gamepage.xaml.cs where the gameplay takes place (like to a mainpage.xaml, then back to the gamepage. This does not fix the problem, so I'm assuming the problem is higher up than something inside the gamepage.xaml/gamepage.xaml.cs
I feel like I've got to be doing something painfully stupid/obvious, but I'm a novice programmer, just picking pieces up as I have a need, and this is my first venture into the world of data binding. I would greatly appreciate any suggestions.
Thanks in advance for your time.
I found the problem. I was wrong when I said I was never leaving the GamePage.xaml.cs and Gamepage.xaml.
I reveiwed my code and found that I was actually jumping out to a transition page that lists the details of the next level, then back to GamePage.
Whenever I left the page, strange things would happen to the App._scoresController.Scores. If I created a private _scoresController.Scores within GamePage.xaml.cs and used that instead of the one in App then everything works. It looks like something strange with Scoreloop.