2010-04-22 27 views

Trả lời

17

Bạn có thể Add existing item tệp .resx vào dự án F #, nó sẽ tự động nhận được BuildAction của EmbeddedResource và hoạt động. Các công cụ VS ở đây không phải là tốt như các ngôn ngữ khác được nêu ra, nhưng MSBuild làm tất cả các nâng nặng, vì vậy nó chỉ là vấn đề nhận được đoạn mã XML bên phải vào tập tin .fsproj.

10

Tôi đang sử dụng một .resx trong dự án F # 2.0. Cảm thấy tự do để có một cái nhìn vào nó và tôi hy vọng nó sẽ giúp bạn. Không có lắc lớn nhưng ở đây nó là cho những gì nó có giá trị.

http://github.com/OnorioCatenacci/ExtendedSearch

EDIT: Đối với những gì nó có giá trị, đây là phần có liên quan của tập tin fsproj

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

Và đây là 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> 

Tôi hy vọng điều này loại bỏ bất kỳ tương lai vấn đề với những người không thể tìm thấy điều này trên GitHub.

+0

Cảm ơn, tôi đã đi xuống một con đường tương tự, tay mã hóa một wrapper quản lý tài nguyên. Bây giờ tôi đang tự hỏi nếu nó sẽ bản địa hóa sẽ làm việc ... –

+0

Tôi muốn được quan tâm để nghe nếu bản địa hóa không hoạt động. Tôi chủ yếu sử dụng nguyên nhân .resx tôi muốn làm mới trí nhớ của mình - kỹ năng mã hóa .Net của tôi hơi bị gỉ. –

+0

Tôi nhận được lỗi 404 sau liên kết tới github: ( – Joh

6

Đây là một cách khác. Mặc dù tôi biết đã quá muộn cho câu hỏi ban đầu, tôi hy vọng nó sẽ giúp người khác.

  1. Tạo tệp văn bản rõ ràng với định dạng tên = giá trị, một cặp khóa/giá trị trên mỗi dòng. Trong một file có tên "strings.txt", viết

    name1=hello 
    name2=world 
    
  2. Tạo một tập tin tài nguyên sử dụng ResGen.exe tác động lên tập tin văn bản từ Bước 1. Bạn có thể tìm hiểu về ResGen tại http://msdn.microsoft.com/en-us/library/ccec7sz1%28v=vs.80%29.aspx. ResGen sẽ tạo một tệp nhị phân CLR có tên là "strings.resources". Đặt tệp tài nguyên này, nơi trình biên dịch của bạn có thể tìm thấy nó.

  3. Thêm "--resource: strings.resources" làm tùy chọn trình biên dịch. Tôi đã thực hiện việc này từ hộp văn bản "Cờ khác" trong thuộc tính Xây dựng. Bạn có thể tìm thêm thông tin tại http://msdn.microsoft.com/en-us/library/dd233171.aspx

  4. Viết sau trong # dự án F

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

Để thêm tài nguyên vào sự F # dự án hiện có, mỗi @ "Onorio Catenacci" GitHub Linke trên ...

  1. Tạo tệp văn bản và điền vào bằng nội dung bên dưới rồi lưu và đóng.
  2. Đổi tên phần mở rộng tập tin văn bản này để .resx (tập tin tài nguyên XML)
  3. Cập nhật các Build Action-EmbeddedResource
  4. Các tập tin lúc đó sẽ mở một cách chính xác trong trình soạn thảo nguồn VS cho phép bạn chỉnh sửa nội dung của nó.

.resx tập tin mẫu:

<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> 
Các vấn đề liên quan