简体   繁体   English

XNA-根据目标平台有条件地包含对象

[英]XNA - Conditionally include object based on target platform

I'm building an XNA 4.0 app for both Windows and the xbox. 我正在为Windows和xbox构建XNA 4.0应用程序。

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. 在Windows版本中,我有一个调试控制台,该控制台引用IronPython和紧凑框架中不支持的某些CLR / DLR程序集。 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. 我知道我可以从xbox项目中删除引用以保持兼容性。 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. 问题是这些类由需要它们的其他类实例化,但仅适用于Windows。

The obvious solution to this quandary is just to wrap calls to the unsupported classes in #if preprocessor directives: 解决这一难题的明显方法是将对不支持的类的调用包装在#if预处理器指令中:

#if WINDOWS
  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: 如果您将Game Studio的内置功能用于“为Xbox 360创建项目的副本”(Zune和Windows Phone的同上),则以下操作将在项目之间进行镜像:

  • 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. 在MSDN上的“ 跨平台游戏项目转换器”页面上对此进行了更详细的说明。 This process is controlled by the <XnaCrossPlatformGroupID> element in the project files. 此过程由项目文件中的<XnaCrossPlatformGroupID>元素控制。

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. 我个人更喜欢在平台缺少某些功能的情况下创建备用存根类,而不是在整个项目中分散使用#ifdef And if you attach a [Conditional("NEVER_DEFINED")] attribute your stub functions, then the compiler will remove calls to them. 并且,如果您在存根函数中附加了[Conditional("NEVER_DEFINED")]属性,则编译器将删除对其的调用。

I am reasonably certain that there is no solution-wide way of specifying define constants in Visual Studio. 我可以肯定地说,在Visual Studio中没有解决方案范围的指定定义常量的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM