Can I use .resx files in F# 2.0 projects?
我可以在f# 2.0项目中使用.resx文件吗?
If so, how do I go about adding, and then using these resources.
如果是,我该如何添加,然后使用这些资源。
Thanks in advance.
提前谢谢。
4 个解决方案
#1
17
You can Add existing item
a .resx
file to an F# project, it should automatically get a BuildAction
of EmbeddedResource
and work. The VS tooling here isn't as good as the other languages yet, but MSBuild does all the heavy lifting, so it's just a matter of getting the right snippet of XML into the .fsproj file.
您可以将现有的项目a .resx文件添加到f#项目中,它应该自动地得到一个嵌入和工作的BuildAction。这里的VS工具并不像其他语言一样好,但是MSBuild完成了所有的繁重工作,所以这只是将XML的正确片段放入.fsproj文件中的问题。
#2
10
I'm using a .resx in an F# 2.0 project. Feel free to take a look at it and I hope it helps you. No great shakes but here it is for what it's worth.
我在f# 2.0项目中使用.resx。随便看看,我希望它能帮到你。没有大的震动,但这是为了它的价值。
http://github.com/OnorioCatenacci/ExtendedSearch
http://github.com/OnorioCatenacci/ExtendedSearch
EDIT: For what it's worth, here's the relevant portion of the fsproj file
编辑:对于它的价值,这里是fsproj文件的相关部分。
<ItemGroup>
<Compile Include="assemblyinfo.ExtendedSearch.exe.fs" />
<Compile Include="ExtendedSearch.fs" />
<EmbeddedResource Include="ExtendedSearch.resx" />
</ItemGroup>
And here's ExtendedSearch.resx:
这里是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 &Version:</value>
</data>
<data name="SearchButtonCaption">
<value>&Search</value>
</data>
<data name="CancelButtonCaption">
<value>&Cancel</value>
</data>
</root>
I hope this removes any future issues with people not being able to find this on GitHub.
我希望这能消除人们在GitHub上无法找到的未来问题。
#3
6
Here is another way. Though I know it's too late for the original question, I hope it helps others.
这是另一种方式。虽然我知道对于最初的问题已经太晚了,我希望它能帮助别人。
-
Create a clean text file with a name=value format, one key/value pair per line. In a file named "strings.txt", write
创建一个具有名称=值格式的干净文本文件,每行一个键/值对。在一个名为“strings”的文件中。txt”,写
name1=hello name2=world
-
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.
使用ResGen创建一个资源文件。执行步骤1中的文本文件。您可以在http://msdn.microsoft.com/en- us/library/ccec7sz1%28v=vs.8029.aspx了解到ResGen。ResGen将创建一个名为“strings.resources”的CLR二进制文件。把这个资源文件放在编译器可以找到的地方。
-
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
添加”——资源:字符串。资源“作为一个编译器选项。我在构建属性中的“其他标志”文本框中这样做了。您可以在http://msdn.microsoft.com/en-us/library/dd233171.aspx找到更多信息。
-
Write the following in your F# project
在你的f#项目中写下以下内容。
open System.Resources let res = ResourceManager("strings", System.Reflection.Assembly.GetExecutingAssembly()) res.GetString("name1") + res.GetString("name2") |> printfn "%s"
#4
4
To add a resource to an existing F# project, per @"Onorio Catenacci" GitHub Linke above...
要将资源添加到现有的f#项目中,每个@“Onorio Catenacci”GitHub Linke…
- Create a text file and fill it with the content below then save and close it.
- 创建一个文本文件并将其填充到下面的内容中,然后保存并关闭它。
- Rename this text file extension to
.resx
(XML resource file) - 将此文本文件扩展名重命名为.resx (XML资源文件)
- Update the
Build Action
toEmbeddedResource
- 将构建操作更新到嵌入式设备。
- The file should now open correctly in the VS resource editor allowing you to edit it's contents.
- 该文件现在应该在VS资源编辑器中正确打开,允许您编辑它的内容。
.resx
file template:
. 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>
#1
17
You can Add existing item
a .resx
file to an F# project, it should automatically get a BuildAction
of EmbeddedResource
and work. The VS tooling here isn't as good as the other languages yet, but MSBuild does all the heavy lifting, so it's just a matter of getting the right snippet of XML into the .fsproj file.
您可以将现有的项目a .resx文件添加到f#项目中,它应该自动地得到一个嵌入和工作的BuildAction。这里的VS工具并不像其他语言一样好,但是MSBuild完成了所有的繁重工作,所以这只是将XML的正确片段放入.fsproj文件中的问题。
#2
10
I'm using a .resx in an F# 2.0 project. Feel free to take a look at it and I hope it helps you. No great shakes but here it is for what it's worth.
我在f# 2.0项目中使用.resx。随便看看,我希望它能帮到你。没有大的震动,但这是为了它的价值。
http://github.com/OnorioCatenacci/ExtendedSearch
http://github.com/OnorioCatenacci/ExtendedSearch
EDIT: For what it's worth, here's the relevant portion of the fsproj file
编辑:对于它的价值,这里是fsproj文件的相关部分。
<ItemGroup>
<Compile Include="assemblyinfo.ExtendedSearch.exe.fs" />
<Compile Include="ExtendedSearch.fs" />
<EmbeddedResource Include="ExtendedSearch.resx" />
</ItemGroup>
And here's ExtendedSearch.resx:
这里是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 &Version:</value>
</data>
<data name="SearchButtonCaption">
<value>&Search</value>
</data>
<data name="CancelButtonCaption">
<value>&Cancel</value>
</data>
</root>
I hope this removes any future issues with people not being able to find this on GitHub.
我希望这能消除人们在GitHub上无法找到的未来问题。
#3
6
Here is another way. Though I know it's too late for the original question, I hope it helps others.
这是另一种方式。虽然我知道对于最初的问题已经太晚了,我希望它能帮助别人。
-
Create a clean text file with a name=value format, one key/value pair per line. In a file named "strings.txt", write
创建一个具有名称=值格式的干净文本文件,每行一个键/值对。在一个名为“strings”的文件中。txt”,写
name1=hello name2=world
-
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.
使用ResGen创建一个资源文件。执行步骤1中的文本文件。您可以在http://msdn.microsoft.com/en- us/library/ccec7sz1%28v=vs.8029.aspx了解到ResGen。ResGen将创建一个名为“strings.resources”的CLR二进制文件。把这个资源文件放在编译器可以找到的地方。
-
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
添加”——资源:字符串。资源“作为一个编译器选项。我在构建属性中的“其他标志”文本框中这样做了。您可以在http://msdn.microsoft.com/en-us/library/dd233171.aspx找到更多信息。
-
Write the following in your F# project
在你的f#项目中写下以下内容。
open System.Resources let res = ResourceManager("strings", System.Reflection.Assembly.GetExecutingAssembly()) res.GetString("name1") + res.GetString("name2") |> printfn "%s"
#4
4
To add a resource to an existing F# project, per @"Onorio Catenacci" GitHub Linke above...
要将资源添加到现有的f#项目中,每个@“Onorio Catenacci”GitHub Linke…
- Create a text file and fill it with the content below then save and close it.
- 创建一个文本文件并将其填充到下面的内容中,然后保存并关闭它。
- Rename this text file extension to
.resx
(XML resource file) - 将此文本文件扩展名重命名为.resx (XML资源文件)
- Update the
Build Action
toEmbeddedResource
- 将构建操作更新到嵌入式设备。
- The file should now open correctly in the VS resource editor allowing you to edit it's contents.
- 该文件现在应该在VS资源编辑器中正确打开,允许您编辑它的内容。
.resx
file template:
. 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>