2010-04-22 25 views

回答

17

您可以將Add existing item a .resx文件轉換爲F#項目,它應該會自動獲得BuildActionEmbeddedResource並開始工作。這裏的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上找到這些問題。

+0

謝謝,我已經下了類似的路徑,手動編碼的資源管理器包裝。我現在想知道它是否會本地化將工作... – 2010-04-23 00:33:07

+0

我會有興趣聽到本地化是否工作。我主要使用.resx因爲我想刷新我的記憶 - 我的.Net編碼技能有點生疏。 – 2010-04-23 00:54:05

+0

我在鏈接到github後收到404錯誤:( – Joh 2013-01-22 12:48:39

6

這是另一種方式。雖然我知道原來的問題已經太晚了,但我希望它能幫助其他人。

  1. 創建一個乾淨的文本文件,其名稱=值格式,每行一個鍵/值對。在一個名爲「strings.txt」文件,寫入

    name1=hello 
    name2=world 
    
  2. 使用作用於從第1步文本文件ResGen.exe您可以在http://msdn.microsoft.com/en-us/library/ccec7sz1%28v=vs.80%29.aspx瞭解RESGEN創建一個資源文件。 ResGen將創建一個名爲「strings.resources」的CLR二進制文件。把這個資源文件放到你的編譯器可以找到的地方。

  3. 將「--resource:strings.resources」添加爲編譯器選項。我在「構建」屬性中的「其他標誌」文本框中執行了此操作。您可以在http://msdn.microsoft.com/en-us/library/dd233171.aspx

  4. 找到更多的信息寫在你的F#項目

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

要將資源添加到現有的F#項目,每@ 「Onorio Catenacci的」 GitHub Linke上面下面...

  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>