Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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)

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top