Hi, Sort of a newbie question, but here goes...
I am trying to use CompositionTarget.Rendering in my WPF 2D game to update the window. However I suck at event handlers etc so nothing happens when I try this and need some help.
I have my MainWindow and a canvas in there where the action is. The game starts when I click a Play button. The Button's click event (in MainWindow.xaml.cs) calls GameController.cs which takes care of the game logic (like the game loop).
A third class, BoardSetup's, DrawGame method is then called from the GameController's GameLoop method and redraws the objects on the canvas. (This works with a DispatcherTimer but not otherwise.) I was advised to use CompositionTarget.Rendering (
c# - How do I refresh the UI smartest in a 2D WPF game? - Stack Overflow[
^]) but don't know how to add it.
So how and where (what file and method (if any) should I add the CompositionTarget.Rendering event handler to make my DrawGame method redraw the game?
Actually I don't care of exactly how often the game renders as long as it's at least say 12 times per second. What's important to me is that the game runs at the same speed regardless of how many objects are in the game (and the gameloop does that today, it's just that the result is never updated onscreen). Therefore I'm throwing out my DispatcherTimer to use CompositionTarget.Rendering instead.
Please help!
What I have tried:
I've tried to use CompositionTarget.Rendering in MainWindow.XAML.cs and in the game loop but the UI is still not redrawn. I have also simply tried myCanvas.UpdateLayout() but it doesn't draw anything onscreen (because of the running gameloop I guess).