简体   繁体   English

C#Windows Phone XNA应用程序中的可选参数

[英]Optional parameters in a C# Windows Phone XNA application

Is it possible to use optional parameters (and other purely-syntactical C# 4.0 features) in Windows Phone XNA applications? 是否可以在Windows Phone XNA应用程序中使用可选参数(以及其他纯语法C#4.0功能)?

I've read and heard conflicting information about this. 我已经阅读并听到有关此事的相互矛盾的信息。 In the Advanced Build Settings for my application, the Language Version is set to C# 3.0 (C# 4.0 is not available in the Language Version drop-down list). 在我的应用程序的高级构建设置中,语言版本设置为C#3.0(语言版本下拉列表中没有 C#4.0)。 Is 3.0 the "official" C# version that's going to be used initially for Windows Phone development? 3.0是最初用于Windows Phone开发的“官方”C#版本吗?

Am I silly for even expecting C# 4.0 features to be available at all? 我是否愚蠢甚至期待C#4.0功能可用?

Beneath the Silverlight and XNA flair lies some version of the .NET 3.x (3.7?) compact framework. Silverlight和XNA的优势在于.NET 3.x(3.7?)紧凑框架的某些版本。 As far behind as Windows Phone 7 is with respect to the rest of the marketplace, this far into the game it's unlikely that a .NET 4.0 compact framework will be inserted (afaik does not yet exist) or any other major architectural changes required to support C# 4.0. 远远落后于Windows Phone 7与市场的其他部分相比,在游戏中,不太可能插入.NET 4.0紧凑框架(afaik尚不存在)或支持所需的任何其他主要架构变更C#4.0。

I have found that Optional Parameters work properly in Silverlight but not in XNA. 我发现可选参数在Silverlight中正常工作,但在XNA中没有。 I have been using the #if stuff to get around this in shared code as follows: 我一直在使用#if的东西在共享代码中解决这个问题,如下所示:

#if !SILVERLIGHT
public SomeClass ( ) : this (null)
{
}

public SomeClass(object someParam)

#else

public SomeClass(object someParam = null)

#endif
{
    m_someParam = someParam;
}

But that is a touch ugly. 但这是一个丑陋的触摸。 I have also noticed the Add References dialog from the Productivity Power Pack doesn't work with XNA projects, indicating something fishy is going on with XNA projects. 我还注意到Productivity Power Pack中的Add References对话框不能与XNA项目一起使用,这表明XNA项目正在发生一些问题。 [As a side note, I can deploy and debug Silverlight apps to the emulator via VS but NOT XNA apps] [作为旁注,我可以通过VS但不是XNA应用程序将Silverlight应用程序部署和调试到模拟器]

Best solution: make use of function overloading. 最佳解决方案:利用函数重载。

Example (causes errors): 示例(导致错误):

public void RenderRadius(SpriteBatch spriteBatch, Entity entity, float radiusOverride = -1)

... ...

Solution: 解:

public void RenderRadius(SpriteBatch spriteBatch, Entity entity){ RenderRadius(spriteBatch, entity, -1); }
public void RenderRadius(SpriteBatch spriteBatch, Entity entity, float radiusOverride)

... ...

Super easy. 超级容易。 Done. 完成。

I'm not sure if it is possible to use the optional parameters with Xna (I've never had the desire to), but you can do what you are talking about. 我不确定是否可以在Xna中使用可选参数(我从来没有想过),但你可以做你正在谈论的事情。

http://xboxforums.create.msdn.com/forums/p/54007/515654.aspx http://xboxforums.create.msdn.com/forums/p/54007/515654.aspx

At the link above, it says to set your language version to default, rather than C# 3.0. 在上面的链接中,它表示将语言版本设置为默认值,而不是C#3.0。 Seems you were on the right track. 看起来你走在正确的轨道上。

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

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