在整个 Silverlight C# 项目中使用常量字符串的正确方法是什么?

发布于 2025-01-08 15:48:55 字数 147 浏览 5 评论 0原文

我希望在整个项目中都有一个恒定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋叶绚丽 2025-01-15 15:48:55

为您可能需要从解决方案中的所有项目访问的内容(例如常量、扩展方法、实用程序等)创建一个 .Common 项目,在其中只需使用您可能需要的任何常量创建 Constants 类。

像这样:

public static class Constants
{
    #region Nested type: Urls

    public static class Urls
    {
        public static readonly string MyUrl = "http://blablabla.com";
    }

    #endregion

}

用法是:

Constants.Urls.MyUrl

祝你好运。

编辑注释:根据 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:

public static class Constants
{
    #region Nested type: Urls

    public static class Urls
    {
        public static readonly string MyUrl = "http://blablabla.com";
    }

    #endregion

}

Usage would be:

Constants.Urls.MyUrl

Good luck.

Edit Note: Changed to const as per Gabes suggestion

Edit Note2: Changed to static readonly per lukas suggestion

眼睛会笑 2025-01-15 15:48:55

这是关于在 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

蓝戈者 2025-01-15 15:48:55

如果您希望 Xaml 和 C# 都可以访问该 URL,并且您需要整个项目而不是整个解决方案都可以访问该 URL。在 App.xaml 中创建静态资源,例如

<System:String x:Key="kURL">"http://www.myurl.com/"</System:String>

定义命名空间“System”

xmlns:System="clr-namespace:System;assembly=mscorlib"

现在,您可以在 xaml 和 C# 代码中使用它。

在 C# 代码中,您可以使用

App.Current.Resources["kURL"];

在 Xaml 中,假设您是否需要用于 textBlock

<TextBlock Text="{StaticResource kURL}" Name="textBlock1" />

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

<System:String x:Key="kURL">"http://www.myurl.com/"</System:String>

define the namespace "System"

xmlns:System="clr-namespace:System;assembly=mscorlib"

Now, you can use this both in xaml and c# code.

In C# code you can use

App.Current.Resources["kURL"];

In Xaml, lets say if you need to use for a textBlock

<TextBlock Text="{StaticResource kURL}" Name="textBlock1" />
忱杏 2025-01-15 15:48:55

您在应用程序类中定义变量并使用它。
由于 App 类是主要的并且可以通过应用程序使用

You define variable in app class and Make use of it.
As App class is the main and is available thought the application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文