0

I'm trying to develop a cross platform application. Currently I have set up following solutions, with their platform specified template:

  • GameBase
  • Windows
  • Xbox
  • WindowsPhone

The Windows, Xbox and WindowsPhone uses the GameBase as reference, but I want to separate different code for each platform used in the GameBase library as the example below:


    #if WINDOWS

    graphics.PreferredBackBufferWidth = 640;
    graphics.PreferredBackBufferHeight = 480;
    graphics.ApplyChanges();

    #elif WINDOWS_PHONE

    TargetElapsedTime = TimeSpan.FromTicks(333333);
    InactiveSleepTime = TimeSpan.FromSeconds(1);

    #endif

It does not work, and I can see that in the Configuration Manager, that GameBase's platform is Any CPU, so I wonder if it has anything to due with the GameBase being a Class Library?

If so how do I fix it, and if possible I would like to keep the #if tags.

Directory setup | Windows Solution Game.cs | Solution Explorer Directory setup | Windows Solution Game.cs | Solution Explorer

ctacke
  • 66,480
  • 18
  • 94
  • 155
Basic
  • 193
  • 1
  • 2
  • 14
  • Most of the App Hub samples have versions for all three. [Check out how they do it](http://create.msdn.com/en-US/education/catalog/sample/simple_animation). They only have one version of each source file; they simply just have a different project per target, all in the same root folder (that way no Links are needed). –  Mar 08 '12 at 13:33

3 Answers3

3

You have to create separate Visual Studio projects for the different platforms. You can reuse code across multiple projects by adding the source using Add As Link. Within a shared source file that is linked to multiple project you can use #if etc. to separate platform specific code.

Add As Link screenshot

How "It does not work" is not clear from your question but in general you cannot build an assembly (e.g. class library) for Windows and then use it on XBox and Windows Phone.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • I have 4 separate Visual Studio projects, 1 being the shared (Class Library) and the other 3 for each different platform. i used the #if in the Class Library, and it does not work. I would like all code to be written in the class library, and shared among the other projects, without having to edit em. – Basic Mar 07 '12 at 15:47
  • By using the Add As Link, I need to add all files from the GameBase as links to the 3 platform specific solutions? After that the #if tags should work? – Basic Mar 07 '12 at 16:01
  • @Basic: Yes, you cannot reuse a single class library project across different platforms (well, sometimes you can but I don't think with XBox and Windows Phone in the mix). However, you can reuse files by using "Add As Link". – Martin Liversage Mar 07 '12 at 16:10
  • @Matin Tested it on Windows and Windows Phone and it works! Thanks a lot :) – Basic Mar 07 '12 at 16:32
1

Sharing projects between different project types is a tricky thing to do - just because of the way Visual Studio project types are used and because the different project types actually need to reference different versions of the dot net framework.

There are a few things that can assist:

Hope that helps a bit. Personally I'm currently mainly using manual cut and paste and hoping that the next version of the Portable Class Library helps (it is due for Win8 Metro stuff)

Stuart
  • 66,722
  • 7
  • 114
  • 165
0

Not sure if this is your only problem but you mispelled WINDOWS_PHONE:

WINDOWS_HPONE

should be:

WINDOWS_PHONE

UPDATE:

The way that I would do it is I would create a class library solution calling it something like GameBaseLibrary. Then create a project there called GameBase and add all code in there that you would like to share across all the platforms. (I.E. Physics, data access, etc...)

Then in that same solution add XboxBaseLibrary, WindowsBaseLibrary, WindowsPhoneBaseLibrary and add code which is specific to each platform, add a reference in each of those projects to the GameBase project to use code from there in each of the three other projects.

Then create 3 different solutions types - Xbox, Window, Windows Phone and add the compiled dlls from GameBaseLibrary as a reference in each of them to be able to use the code. As you're working on each version of code (xbox, windows, windows phone) pay attention to code that you're typing to ensure that it's not something which can live within the GameBase class because you don't want to keep rewriting the same code.

This way, the bulk of the code is sitting in GameBaseLibrary and the platform specific code is sitting within each of the 3 other projects.

I hope this is clear.

evasilchenko
  • 1,862
  • 1
  • 13
  • 26
  • This is just from my example :) Currently I'm testing at the #WINDOWS tag and it does not run anything inside it. – Basic Mar 07 '12 at 15:35
  • Sorry, but you will have to provide a little bit more clarity around the exact issue and your overall project set up. It's unclear if GameBase is a class or a class library project within visual studio. – evasilchenko Mar 07 '12 at 15:40
  • GameBase is a Class Library project, take a look at the picture I uploaded :) – Basic Mar 07 '12 at 15:56