2013-01-04 115 views

回答

24

打開resx文件,在它的工具欄上有一個Access Modifier菜單。設置爲Public。這將生成一個*.Designer.cs文件。

enter image description here

+3

當人們重命名resx時,項目文件/ TFS有時會感到困惑。只需刪除.cs文件並使用此功能重新創建它就像魅力一樣。 – MartijnK

+2

設置Access公共訪問修飾符完美無缺 - 謝謝。 – Mani5556

+0

保存Resources.resx就足夠了,不需要更改Access修飾符。 –

3

如果文件被添加到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"; 
} 

我已經夷爲平地,我真正的解決方案上面的代碼一點點,因爲我使用自定義類的每個畫面,而不是簡單的文件名還可以在不同的子文件夾中支持相同的文件名(通過使用命名空間生成的部分文件夾結構)。但對於第一槍,上述應該可以幫到你。

0

這也適用於我:雙擊並打開resx文件,添加一個虛擬資源,點擊保存。生成.designer.cs文件。

+0

保存「Resources.resx」就足夠了,不需要虛擬資源。 –

0

你也可以這樣做代碼: (從這裏摘自: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

0

如果你刪除它或添加它的.gitignore因爲你認爲你不需要它。這是如何重新生成文件。

轉到訪問修飾符和它(公共/內部),以「無代碼生成」

改變現在把它放回公共/內部。 VS將爲您重新生成Designer文件。

+0

保存'Resources.resx'就足夠了,不需要改變'Access modifier'。 –

1

右鍵單擊Resources.resx並選擇「運行自定義工具」。

+0

這應該是正確的答案! –