如何在F#项目中管理资源?

21

我可以在 F# 2.0 项目中使用 .resx 文件吗?

如果可以,那么我该如何添加和使用这些资源呢?

提前感谢。

4个回答

17

您可以将一个 .resx 文件添加到 F# 项目中,它应该会自动获得 BuildAction 设置为 EmbeddedResource 并工作。这里的VS工具还不如其他语言的好,但是MSBuild会处理所有繁重的工作,所以只需要将正确的XML代码片段插入到 .fsproj 文件中即可。


10
我正在一个F# 2.0项目中使用.resx文件。请随意查看它,希望对你有所帮助。虽然并不是很了不起,但这就是它的价值所在。

http://github.com/OnorioCatenacci/ExtendedSearch

编辑:值得一提的是,这是fsproj文件的相关部分

  <ItemGroup>
    <Compile Include="assemblyinfo.ExtendedSearch.exe.fs" />
    <Compile Include="ExtendedSearch.fs" />
    <EmbeddedResource Include="ExtendedSearch.resx" />
  </ItemGroup>

这里是ExtendedSearch.resx:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, ...</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, ...</value>
  </resheader>
  <!-- 
    Microsoft ResX Schema 

    Version 2.0

    The primary goals of this format is to allow a simple XML format 
    that is mostly human readable. The generation and parsing of the 
    various data types are done through the TypeConverter classes 
    associated with the data types.

    Example:

    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>[base64 mime encoded serialized .NET Framework object]</value>
    </data>
    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
        <comment>This is a comment</comment>
    </data>

    There are any number of "resheader" rows that contain simple 
    name/value pairs.

    Each data row contains a name, and value. The row also contains a 
    type or mimetype. Type corresponds to a .NET class that support 
    text/value conversion through the TypeConverter architecture. 
    Classes that don't support this are serialized and stored with the 
    mimetype set.

    The mimetype is used for serialized objects, and tells the 
    ResXResourceReader how to depersist the object. This is currently not 
    extensible. For a given mimetype the value must be set accordingly:

    Note - application/x-microsoft.net.object.binary.base64 is the format 
    that the ResXResourceWriter will generate, however the reader can 
    read any of the formats listed below.

    mimetype: application/x-microsoft.net.object.binary.base64
    value   : The object must be serialized with 
            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.soap.base64
    value   : The object must be serialized with 
            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.bytearray.base64
    value   : The object must be serialized into a byte array 
            : using a System.ComponentModel.TypeConverter
            : and then encoded with base64 encoding.
    -->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <data name = "AppDisplayName">
    <value>Extended Search</value>
  </data>
  <data name="FilePathSpecLabel">
    <value>Base File Path:</value>
  </data>
  <data name="FilePathSpecDefault">
    <value>C:\</value>
  </data>
  <data name ="SelectDirButtonLabel">
    <value>. . .</value>  
  </data>  
  <data name="FileNameSpecLabel">
    <value>File Name Spec:</value>
  </data>
  <data name="FileNameSpecDefault">
    <value>*.dll</value>
  </data>
  <data name="RecurseIntoSubdirsLabel">
    <value>Recurse Into Subdirectories</value>
  </data>
  <data name ="MajorVerDefault">
    <value>1</value>
  </data>
  <data name="MinorVerDefault">
    <value>0</value>
  </data>
  <data name="RevisionVerDefault">
    <value>0</value>
  </data>
  <data name="BuildVerDefault">
    <value>0</value>
  </data>
  <data name="VersionLabel">
    <value>File &amp;Version:</value>
  </data>
  <data name="SearchButtonCaption">
    <value>&amp;Search</value>
  </data>
  <data name="CancelButtonCaption">
    <value>&amp;Cancel</value>
  </data>
</root>

我希望这可以避免未来出现人们在GitHub上找不到这个问题。

@OnorioCatenacci 没有,还是没有运气。它在 https://github.com/OnorioCatenacci?tab=repositories 的项目列表中没有列出。 - Joh
抱歉@Joh,我忘记了我在发布原始答案后将存储库设为私有。现在应该可以看到了。虽然它不是我写过的最好的代码,但它应该可以帮助你处理.resx文件。 - Onorio Catenacci
现在我能看到了,谢谢。我相信确切的行号是 https://github.com/OnorioCatenacci/ExtendedSearch/blob/master/ExtendedSearch/ExtendedSearch.fsproj#L41。 - Joh
你得到了404错误,因为我删除了仓库。我会看看能否找到代码并将其粘贴到答案中。 - Onorio Catenacci
@DanBarowy 我已经在答案中添加了我认为相关的代码部分。但我认为George的答案也涵盖了这一点。 - Onorio Catenacci
显示剩余2条评论

6

这里还有另一种方法。虽然我知道对于原问题来说已经太晚了,但希望能帮助其他人。

  1. Create a clean text file with a name=value format, one key/value pair per line. In a file named "strings.txt", write

    name1=hello
    name2=world
    
  2. Create a resource file using ResGen.exe acting on your text file from Step 1. You can learn about ResGen at http://msdn.microsoft.com/en-us/library/ccec7sz1%28v=vs.80%29.aspx. ResGen will create a CLR binary file named "strings.resources". Put this resource file where your compiler can find it.

  3. Add "--resource:strings.resources" as a compiler option. I did this from the "Other flags" textbox in the Build properties. You can find more info at http://msdn.microsoft.com/en-us/library/dd233171.aspx

  4. Write the following in your F# project

    open System.Resources
    let res = ResourceManager("strings", System.Reflection.Assembly.GetExecutingAssembly())
    res.GetString("name1") + res.GetString("name2") |> printfn "%s"
    

4
要将资源添加到现有的 F# 项目中,根据 @"Onorio Catenacci" 的 GitHub 链接,请按照以下步骤操作:
  1. 创建一个文本文件并填写下面的内容,然后保存并关闭它。
  2. 将此文本文件扩展名更改为 .resx(XML 资源文件)。
  3. 更新 Build ActionEmbeddedResource
  4. 现在,该文件应正确打开在 VS 资源编辑器中,允许您编辑其内容。

.resx 文件模板:

    <root>
      <resheader name="resmimetype"><value>text/microsoft-resx</value></resheader>
      <resheader name="version"><value>2.0</value></resheader>
      <resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, ...</value></resheader>
      <resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, ...</value></resheader>
      <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
        <xsd:element name="root" msdata:IsDataSet="true">
          <xsd:complexType>
            <xsd:choice maxOccurs="unbounded">
              <xsd:element name="metadata">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" />
                  </xsd:sequence>
                  <xsd:attribute name="name" use="required" type="xsd:string" />
                  <xsd:attribute name="type" type="xsd:string" />
                  <xsd:attribute name="mimetype" type="xsd:string" />
                  <xsd:attribute ref="xml:space" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="assembly">
                <xsd:complexType>
                  <xsd:attribute name="alias" type="xsd:string" />
                  <xsd:attribute name="name" type="xsd:string" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="data">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                    <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
                  <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
                  <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
                  <xsd:attribute ref="xml:space" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="resheader">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:choice>
          </xsd:complexType>
        </xsd:element>
      </xsd:schema>
    </root>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接