Embed Spine animations in Xamarin.Forms using Monogame

I’m trying to embed an animated avatar that was created using Spine in Xamarin.Forms.

The idea is as follows: I want to create a Xamarin.Forms UI (XAML) in a PCL, instantiate a Game class of the Monogame Framework (Microsoft.Xna.Framework.Game) and have that game instance render on the Xamarin.Forms View.

I have managed to get a Spine animation to run on Xamarin.Android so far.

In the Android project, I can request an “Android View” service type from the Monogame game class, like that:

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

var g = new Game1(); //inherits from Microsoft.Xna.Framework.Game
SetContentView((View)g.Services.GetService(typeof(View)));
g.Run();

}
Is there a generic view type I can request from the Game class as to take that view and have it render on a Xamarin.Forms page? (That way I could leave all animation-related code in the PCL and have it render in a cross-platform style.)

Or is there any other way I can integrate the animation in Xamarin.Forms? The animation should exist alongside other Xamarin.Forms controls like buttons or a menu.

Any help would be greatly appreciated!

Thank you,
Christoph

2 Likes

I’d love to be able to mix MonoGame with Xamarin.Forms in a project. Are there any examples of this working?

Unfortunately I haven’t found any examples so far. I’d like to share my thoughts on this…

I’m currently trying to get this to work and have built MonoGame from source to find out more. The problem I’m facing is this:

On the one hand, the Android Activity has to inherit from “global::Xamarin.Forms.Platform.Android.FormsApplicationActivity” to load the PCL app containing the XAML (via LoadApplication). On the other hand, in order to get MonoGame to run, the same activity would have to inherit from MonoGame’s “Microsoft.Xna.Framework.AndroidGameActivity.” As I have not found another way of loading the XAML application, I think the only way to go is to inherit from FormsApplicationActivity and create the required instance for MonoGame somehow by extending/modifying/tricking MonoGame.

I’ll let you know if I’m successful with this approach. Feel free to share your thoughts on this as well.

@christoph, did you got this working ?

I wrote a new BaseClass to use for the Activity which inherits from AndroidGameActivity and pulls in all the MonoGame functionality via the MonoGame Interfaces (so it basically behaves like Microsoft.Xna.Framework.AndroidGameActivity).

You can find it here:

Then in your activity where you want to embed the animation, derive from this BindableMonogameActivity.
There you can initialize and start your animation (RunGame) which itself derives from Microsoft.Xna.Framework.Game. You can grab the animations view via animation.GetView() and add it to some placeholder container on the activity like so: animationPlaceholder.AddView(view);

Hope this helps.

Thanks @christoph, This will certainly help if I go for traditional approach of Xamarin.Android & Xamarin.iOS.

What I am looking for getting this working in Xamarin forms :slight_smile: