XNA - Conditionally include object based on target platform
I'm building an XNA 4.0 app for both Windows and the xbox.
In the Windows version, I have a debug console that references IronPython and some CLR/DLR assemblies that are not supported in the compact framework. I've also got a few partial classes that reference those items.
I know that I can remove references from the xbox project to maintain compatibility. However, I also need to remove the classes that reference those items. The issues is that those classes are instantiated by other classes that require them, but only for Windows.
The obvious solution to this quandary is just to wrap calls to the unsupported classes in #if
preprocessor directives:
#if WINDOWS
开发者_JS百科 InitializeScriptingEngine(....);
#endif
Also, I suppose I could wrap an entire class/partial class the same way to keep it from being compiled. Is there a better way to do this? I'd like to avoid peppering the code with preprocessor directives.
Also, I can't seem to find a way to specify conditionals on a solution-wide basis.
If you're using Game Studio's built-in functionality for "Create copy of project for Xbox 360" (ditto for Zune and Windows Phone), then the following operations will be mirrored between projects:
- Add New Item
- Add Existing Item
- Rename
- Delete
Crucially, the following operations are not mirrored between projects:
- Exclude From Project
- Changing references and content references
This is explained (with a lot more detail) on the Cross-Platform Game Project Converter page on MSDN. This process is controlled by the <XnaCrossPlatformGroupID>
element in the project files.
The upshot is that you can simply exclude the files containing the irrelevant classes from your project on that platform.
Personally I prefer, instead of scattering #ifdef
throughout my project, to create alternate stub classes in cases where a platform is missing some functionality. And if you attach a [Conditional("NEVER_DEFINED")]
attribute your stub functions, then the compiler will remove calls to them.
I am reasonably certain that there is no solution-wide way of specifying define constants in Visual Studio.
精彩评论