简体   繁体   English

如何处理Xamarin Forms中设置的(常量)字符串

[英]How to handle (constant) string set in Xamarin Forms

I have a Xamarin Forms project. 我有一个Xamarin Forms项目。 I would like to organize different strings placed in many classes: how is best pattern to handle constant string? 我想组织放置在许多类中的不同字符串:如何处理常量字符串的最佳模式? (maybe the question, if Xamarin doesn't support it, is related to generic .net application). (也许这个问题,如果Xamarin不支持它,则与通用.net应用程序有关)。

Static class with get/set string properties? 具有获取/设置字符串属性的静态类? Enum of strings? 枚举字符串? (i haven't tried yet) (我还没有尝试)

Thank you for any suggestion! 感谢您的任何建议!

Lewix Lewix

If you're talking about application wide constant strings that represent information that configure the context that they are in (ie connectionstrings, URL's, etc) then you can put store those in the properties of your application. 如果您在谈论应用程序范围内的常量字符串,这些字符串代表配置它们所处上下文的信息(即连接字符串,URL等),则可以将其存储在应用程序的属性中。 This way you can change the configuration outside of your solution as well, by changing the config file. 这样,您还可以通过更改配置文件来在解决方案之外更改配置。

If you do not want to store the data in a configuration file, then storing them in a static class is not a terrible option, I know of people that like to define such constants in their Program class (eg Program.Constants.<constant> . I myself choose to only define constants where I need them, rather than exposing them to the entire application, unless they are configuration variables. In that case I use the properties of the application. 如果您不希望将数据存储在配置文件中,那么将它们存储在静态类中并不是一个糟糕的选择,我知道有些人喜欢在其Program类中定义此类常量(例如Program.Constants.<constant>我自己选择只在需要的地方定义常量,而不是将它们公开给整个应用程序,除非它们是配置变量,在这种情况下,我将使用应用程序的属性。

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx

I prefer using .Net resources files (resx) for strings. 我更喜欢使用.Net资源文件(resx)作为字符串。 This makes it easier to locate application strings in the project and internationalization is natively supported for all platforms without platform-specific code or resources, so no Localizable.plist on iOS or strings file on Android. 这样可以更轻松地在项目中定位应用程序字符串,并且所有平台都固有地支持国际化,而无需特定于平台的代码或资源,因此iOS上没有Localizable.plist或Android上没有strings文件。 This also helps to non-technical team members to modify these files with one of the many existing resx editors . 这也有助于非技术团队成员使用许多 现有的 resx 编辑器之一来修改这些文件。

In addition, you get static class with constants for all defined strings. 另外,您将获得带有所有已定义字符串的常量的 静态类

Example extracted from the xamarin docs : xamarin docs中提取的示例

AppResources.resx : AppResources.resx

<data name="NotesLabel" xml:space="preserve">
    <value>Notes:</value>
    <comment>label for input field</comment>
</data>
<data name="NotesPlaceholder" xml:space="preserve">
    <value>eg. buy milk</value>
    <comment>example input for notes field</comment>
</data>
<data name="AddButton" xml:space="preserve">
    <value>Add new item</value>
</data>

Usage in code: 在代码中的用法:

myLabel.Text = AppResources.NotesLabel;
myEntry.Placeholder = AppResources.NotesPlaceholder;
myButton.Text = AppResources.AddButton;

Obtaining a consistent behavior on all platforms: 在所有平台上获得一致的行为:

在此处输入图片说明

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

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