在整个 Silverlight C# 项目中使用常量字符串的正确方法是什么?
我希望在整个项目中都有一个恒定的 string kURL = "http://www.myurl.com/";
。在 Windows Phone 7 应用程序中执行此操作的正确方法是什么?
我应该把这段代码放在哪里?正确的语法应该是什么?
I want to have a constant string kURL = "http://www.myurl.com/";
throughout my entire project. What's the proper way to do this in a Windows Phone 7 app?
Where would I put this code and what should the proper syntax be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为您可能需要从解决方案中的所有项目访问的内容(例如常量、扩展方法、实用程序等)创建一个 .Common 项目,在其中只需使用您可能需要的任何常量创建 Constants 类。
像这样:
}
用法是:
祝你好运。
编辑注释:根据 Gabes 建议更改为 const
编辑注释2:根据 卢卡斯建议
Create a .Common project for things that you may need to access from all of your projects in your solution (like constants, extension methods, utils etc.), in there simply create Constants class with any Constants that you may need.
Like this:
}
Usage would be:
Good luck.
Edit Note: Changed to const as per Gabes suggestion
Edit Note2: Changed to static readonly per lukas suggestion
这是关于在 C# 中创建全局常量的简单教程。我已将其用于 .NET,但未用于 Windows Phone。我假设遵循相同的约定。
http://www.dotnetperls.com/global-variable
Here is a simple tutorial on creating Global Consts in C#. I've used this for .NET, but not Windows Phone. I would assume the same conventions are followed.
http://www.dotnetperls.com/global-variable
如果您希望 Xaml 和 C# 都可以访问该 URL,并且您需要整个项目而不是整个解决方案都可以访问该 URL。在 App.xaml 中创建静态资源,例如
定义命名空间“System”
现在,您可以在 xaml 和 C# 代码中使用它。
在 C# 代码中,您可以使用
在 Xaml 中,假设您是否需要用于 textBlock
If you want the URL be accessible to both Xaml and C# and if you need this to be accessible to whole project and not whole solution. create a static resource in App.xaml like
define the namespace "System"
Now, you can use this both in xaml and c# code.
In C# code you can use
In Xaml, lets say if you need to use for a textBlock
您在应用程序类中定义变量并使用它。
由于 App 类是主要的并且可以通过应用程序使用
You define variable in app class and Make use of it.
As App class is the main and is available thought the application