1

why can't I use regular class libraries in XBox360 games?

I have application logic which I want to keep independent from XNA and use in both WPF and XNA applications.

Does anyone know good practice to share code between XBox/Phone7 applications and "regular" windows applications?

Max
  • 153
  • 9
  • Using XNA Game Studio, I was able to share my library projects between PC, XBox360 and WP7 without issues. The only trick is using the library *projects* inside your game project, not the DLLs. – jv42 May 01 '11 at 09:24

3 Answers3

4

Have a look at Portable Class Libraries: http://msdn.microsoft.com/en-us/library/gg597391.aspx

Using the Portable Class Library project, you can build portable assemblies that work without modification on the .NET Framework, Silverlight, Windows Phone 7, or XNA (Xbox) platforms. Without the Portable Class Library project, you must target a single platform and then manually rework the class library for other platforms. The Portable Class Library project supports a subset of assemblies from these platforms, and provides a Visual Studio template that makes it possible to build assemblies that run without modification on these platforms.

JoDG
  • 1,326
  • 8
  • 18
  • Sounds like a better solution than mine - I didn't realise this existed! Looks like there are some restrictions on these types of projects, but if you can use it, it's a much nicer way. – Danny Tuppeny Apr 29 '11 at 09:50
  • Thanks, nice solution! I am currently moving everything to portable libraries - unfortunately, my serialization isn't supported anymore. Any experience which serialization method I should use? XML serialization? – Max Apr 29 '11 at 14:24
  • If something isn't support in the Portable Class Libraries, you could try a combination of the mirrored XNA projects (as described in Andrew Russel's answer) and Linked Files into the Silverlight project (my answer) – Danny Tuppeny Apr 30 '11 at 10:29
1

Edit: Check out JoDG's answer for a nicer solution.

You could try linked files...

In your Xbox project, right-click -> Add Existing File, then after selecting the file(s), click the little drop-down arrow on the "Add" button and click "Add As Link", as shown here:

Add As Link

This might not work if you need to share more than just files, but it's an easy way to share code.

Community
  • 1
  • 1
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
1

JoDG's answer is probably the most practically useful. But just to provide additional information:

why can't I use regular class libraries in XBox360 games?

Because the different XNA platforms target different versions of the .NET framework.

XNA Game Studio itself provides a mechanism (documented here and more info here) for automatically mirroring the source files of a project between two projects. This mechanism can be used for creating copies of your library projects for each platform, as well as for your game projects.

For XNA-related work, this is the preferred method for creating cross-platform libraries. When you have to also make your library work on WPF you have to take additional steps:

On Windows, an XNA game is just like any other .NET application, and a XNA library for Windows is just like any other .NET library. Except for the fact that they reference XNA assemblies. So your WPF application can reference a Windows version of your XNA library. But if you want your application to work on systems without XNA installed, you need to remove the XNA assembly references from the Windows project for that XNA library.

XNA Game Studio will still mirror changes between the Windows library and the Xbox 360 library for you.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104