[SOLVED] Cross Platform Solution

First up I am a bit of a noob when it comes to project templates etc.

With Xamarin now being free I thought I would make a small game for android and iOS. I want to avoid having to separate solutions for each platform. I would like to avoid having to code for one platform the copy paste for the other.

From what I understand you are supposed to have one solution with multiple projects. One project for the main chunk of the code, the code that stays the same for all platforms. And then separate projects for each platform which then reference the main project.

The problem I have is this. I have tried referencing a project created with one of the monogame templates (I tried several) but it does not work. Lets say Project A is the main project and I want Project B to reference it. I right click Project B -> Add -> Reference -> Project A. So far so good, no errors or anything. But I can not actually using any of the code from Project A in Project B. The other way around works fine.

For Project A:
I have tried using Monogame Windows OpenGL template, Monogame Cross Platform Desktop template.

For Project B:
Blank App (Android)

I am using VS Community 2015
Monogame 3.5

Maybe you’re forgetting a using statement?

Maybe you’re forgetting to actually put code/references in that are needed to make monogame work on Android

Put all of the references from “C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Android” into your Android project and in the Main Activity change it form inheriting whatever it currently does to AndroidGameActivity and remove all the code in the OnCreate void other than base.OnCreate(bundle); and add the following lines bellow:

var g = new Game1(); SetContentView((View)g.Services.GetService(typeof(View))); g.Run();

so it should like like in whole:
`using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Microsoft.Xna.Framework;
using PongClone;

namespace App1 {
[Activity(Label = “App1”, MainLauncher = true, Icon = “@drawable/icon”)]
public class MainActivity : AndroidGameActivity {

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);
        var g = new Game1();
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();

    }
}

}`

I can also upload a example project if you wish?

If you could upload an example that would be greatly appreciated, but don’t stress. It’s not like this project will be the next super hit on mobile or anything :stuck_out_tongue: I can just create the android app first and port to iOS later if I feel like it.

But I am kinda interested in finding out what I’m doing wrong or if something is wrong with my install. So if you could upload an example that would be great, but as I said don’t stress :slightly_smiling:

Yes you are correct. You will need a separate project for each platform, reference the correct version of MonoGame and add your game code to each project. The best way to do it is put your code in a another location and add the files as links.

What you are asking for is MonoGame bundled in a PCL, which yeah would be awesome. In this scenario, there would be a yourGame.PCL project with all game logic and reference to MonoGame.PCL, and each platform just references that project. There’s still a lot of work to get there.

You can use MG Shared project. The setup would be:

  • Game (Shared Project)
  • Game.DesktopGL (just references the Game project and launches it)
  • Game.Android (just references the Game project and launches it)

Similar stuff can be done with PCL, but MG PCL binaries haven’t been updated since 3.2 :frowning:

If you want to setup shared project, but don’t know how I wouldn’t mind explaining it in more detail, just ask.

1 Like

I think I should be able to figure it out, thanks :slightly_smiling:

Something like this right?

Almost.

You can move Content to shared project as well.

In case you need it, add this to .projitems file:


  <ItemGroup>
    <MonoGameContentReference Include="$(MSBuildThisFileDirectory)Content\Content.mgcb" />
  </ItemGroup>

Ok, thank you very much, I appreciate it!