28

我有一个生成.resx资源文件的程序。这些资源文件用于其他项目,与生成资源文件的项目不在同一个解决方案中。

我现在想知道,是否可以designer.cs从资源文件生成一个文件,以便您可以直接访问资源而无需使用 resxresourcereader。

4

6 回答 6

67

打开 resx 文件,它的工具栏上有一个Access Modifier菜单。将此设置为Public。这将生成一个*.Designer.cs文件。

在此处输入图像描述

于 2013-01-04T09:00:40.930 回答
7

右键单击Resources.resx并选择“运行自定义工具”。

于 2017-07-10T14:17:32.957 回答
6

如果将文件添加到 Visual Studio 项目中,则必须将文件的Custom Tool属性设置为. 然后VS会自动创建需要的设计器文件。.resxResXFileCodeGenerator

在一个项目中,我制作了一个 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";
}

我已经将上面的代码展平了一点,因为在我的实际解决方案中,我为每张图片使用了一个自定义类而不是简单的文件名,以在不同的子文件夹中也支持相同的文件名(通过使用命名空间的文件夹结构的一部分一代)。但是对于第一次拍摄,上述内容应该对您有所帮助。

于 2013-01-04T09:30:59.233 回答
1

您也可以在代码中执行此操作:(取自此处: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

于 2017-03-30T15:25:10.423 回答
0

这也对我有用:双击并打开 resx 文件,添加一个虚拟资源,单击保存。.designer.cs 文件生成。

于 2014-11-05T19:09:46.997 回答
0

如果您删除它或将其添加到 .gitignore 因为您认为您不需要它。这就是您重新生成文件的方式。

转到访问修饰符并将其从(公共/内部)更改为“无代码生成”

现在把它放回公共/内部。VS 将为您重新生成设计器文件。

于 2017-04-04T14:27:47.977 回答