【发布时间】:2012-12-18 16:51:06
【问题描述】:
我有一个生成.resx 资源文件的程序。这些资源文件用于其他项目,与生成资源文件的项目不在同一个解决方案中。
我现在想知道,是否可以从资源文件中生成一个designer.cs 文件,这样你就可以直接访问资源而无需使用 resxresourcereader。
【问题讨论】:
标签: c# visual-studio resx
我有一个生成.resx 资源文件的程序。这些资源文件用于其他项目,与生成资源文件的项目不在同一个解决方案中。
我现在想知道,是否可以从资源文件中生成一个designer.cs 文件,这样你就可以直接访问资源而无需使用 resxresourcereader。
【问题讨论】:
标签: c# visual-studio resx
打开 resx 文件,它的工具栏上有一个 Access Modifier 菜单。将此设置为Public。这将生成一个*.Designer.cs 文件。
【讨论】:
右击Resources.resx并选择“运行自定义工具”。
【讨论】:
如果将文件添加到 Visual Studio 项目中,则必须将 .resx 文件的 Custom Tool 属性设置为 ResXFileCodeGenerator。然后VS会自动创建需要的设计器文件。
在一个项目中,我制作了一个 T4 脚本,用于扫描项目中的文件夹以查找所有图像,然后单击即可创建相应的资源文件。
这是 T4 脚本中需要的部分:
var rootPath = Path.GetDirectoryName(this.Host.TemplateFile);
var imagesPath = Path.Combine(rootPath, "Images");
var resourcesPath = Path.Combine(rootPath, "Resources");
var pictures = Directory.GetFiles(imagesPath, "*.png", SearchOption.AllDirectories);
EnvDTE.DTE dte = (EnvDTE.DTE)((IServiceProvider)this.Host)
.GetService(typeof(EnvDTE.DTE));
EnvDTE.Projects projects = dte.Solution.Projects;
EnvDTE.Project iconProject = projects.Cast<EnvDTE.Project>().Where(p => p.Name == "Icons").Single();
EnvDTE.ProjectItem resourcesFolder = iconProject.ProjectItems.Cast<EnvDTE.ProjectItem>().Where(item => item.Name == "Resources").Single();
// Delete all existing resource files to avoid any conflicts.
foreach (var item in resourcesFolder.ProjectItems.Cast<EnvDTE.ProjectItem>())
{
item.Delete();
}
// Create the needed .resx file fore each picture.
foreach (var picture in pictures)
{
var resourceFilename = Path.GetFileNameWithoutExtension(picture) + ".resx";
var resourceFilePath = Path.Combine(resourcesPath, resourceFilename);
using (var writer = new ResXResourceWriter(resourceFilePath))
{
foreach (var picture in picturesByBitmapCollection)
{
writer.AddResource(picture.PictureName, new ResXFileRef(picture, typeof(Bitmap).AssemblyQualifiedName));
}
}
}
// Add the .resx file to the project and set the CustomTool property.
foreach (var resourceFile in Directory.GetFiles(resourcesPath, "*.resx"))
{
var createdItem = resourcesFolder.Collection.AddFromFile(resourceFile);
var allProperties = createdItem.Properties.Cast<EnvDTE.Property>().ToList();
createdItem.Properties.Item("CustomTool").Value = "ResXFileCodeGenerator";
}
我已经将上面的代码扁平化了一点,因为在我的实际解决方案中,我为每张图片使用了一个自定义类而不是简单的文件名,以便在不同的子文件夹中也支持相同的文件名(通过使用文件夹结构的一部分用于命名空间生成)。但是对于第一次拍摄,上述内容应该对您有所帮助。
【讨论】:
您也可以在代码中执行此操作: (取自这里:msdn)
StreamWriter sw = new StreamWriter(@".\DemoResources.cs");
string[] errors = null;
CSharpCodeProvider provider = new CSharpCodeProvider();
CodeCompileUnit code = StronglyTypedResourceBuilder.Create("Demo.resx", "DemoResources",
"DemoApp", provider,
false, out errors);
if (errors.Length > 0)
foreach (var error in errors)
Console.WriteLine(error);
provider.GenerateCodeFromCompileUnit(code, sw, new CodeGeneratorOptions());
sw.Close();
需要参考system.design.dll
【讨论】:
这也对我有用:双击并打开 resx 文件,添加一个虚拟资源,单击保存。 .designer.cs 文件已生成。
【讨论】:
Resources.resx就足够了,不需要虚拟资源。
如果您因为认为不需要它而将其删除或添加到 .gitignore。这就是您重新生成文件的方式。
转到访问修饰符并将其从(公共/内部)更改为“无代码生成”
现在把它放回公共/内部。 VS 会为你重新生成 Designer 文件。
【讨论】:
Resources.resx就够了,不用改Access modifier。