The messaging system is quite simple. There are a collection of functions and a single event that allow WPF to receive and broadcast arbitrary data of arbitrary types.

Sending Data from WPF to a Game

The functions that can be used to alert a Xnaml Game and send data to it are:

void SendMessage<T> ( object sender, T data );
void SendMessage<T> ( object sender, ref T data );

You can use either function, depending on your needs. When this is invoked on your Game, it will activate a protected virtual function that you can override to deal with whatever Type of data and from whatever sender it came from:

protected virtual void MessageReceived<T> ( object sender, ref T data ) {
	/* Handle the data passed down in this function, or do nothing. */

Simply call the function from the outside with whatever sender and objects on the Game you'd like to alert.

Receiving Data in WPF from an XNA Game

In your game class, call the Game's BroadcastMessage() protected function to get it to trigger the Broadcast event on the game:

protected virtual void BroadcastMessage<T> ( object sender, T data );

/* ... Call it as you need it, inside your game class ... */

BroadcastMessage( this, "Must.... Reach.... 9000!" );

When broadcasted, you can just handle the Broadcast Event, with the Xnaml.BroadcastEventArgs holding the data:

/* Handling the Broadcast Event with a TextBox that shows the broadcast data if its a string */
private void XNAGame_Broadcast ( object sender, BroadcastEventArgs e ) {
	if ( e.BroadcastData is string ) {
		BroadcastReciever.Text = ( e.BroadcastData as string );

And that's it. It's fairly simple. If you need an example, check out the Xnaml.Demo that's packed with the Source Code.

Last edited Apr 26, 2012 at 2:30 AM by ThePhD, version 2


No comments yet.