I am making a little game in WPF to understand it better, I have a parent control that has 2 child usercontrols.
The first child user control has buttons that update the position of an object in the second usercontrol.
In the MainWindow.xaml.cs
I have
controllersplaceholder.Content = new ControllerView1()
gameplaceholder.Content = new GameView1()
As you can see, controllersplaceholder has no knowledge whatsoever of GameView1.
To update a position of an element in GameView1 I would have to pass GameView1 reference into ControllerView1 and execute methods in GameView1().
Question is how can I do it so that
ControllerView1,ControllerView2 will execute methods in GameView1 easily.
You can use events. Simply declare an event on your
ControllerView1, say:When creating your objects, make
GameView1subscribe and react to controller’s event:Now, whenever you raise an event from controller, whoever subscribed will be notified and proper actions will be taken (execution of subscribed methods).
Note that your game view method doesn’t have to match event signature exactly; with a little help from anonymous methods you can create matching event handler on the fly and use already existing methods from
GameView1: