फ़ाइल एक दृश्य स्टूडियो परियोजना में जोड़ा जाता है, तो आप ResXFileCodeGenerator
को .resx
फ़ाइल के Custom Tool
गुण सेट करने के लिए है। फिर वीएस स्वचालित रूप से आवश्यक डिजाइनर फ़ाइल बना देगा।
एक प्रोजेक्ट में मैंने एक टी 4 स्क्रिप्ट बनाई जो सभी छवियों के लिए प्रोजेक्ट के भीतर फ़ोल्डर को स्कैन करता है और एक क्लिक पर एक संबंधित रिसोर्स फ़ाइल बनाने देता है।
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";
}
मैं अपने वास्तविक समाधान में उपरोक्त कोड एक छोटा सा, कारण चपटा मैं सरल फ़ाइल नाम के बजाय प्रत्येक तस्वीर के लिए एक कस्टम वर्ग का उपयोग करें:
यहाँ टी -4 स्क्रिप्ट से बाहर की जरूरत हिस्सा है विभिन्न उप फ़ोल्डरों में उसी फ़ाइल नाम का समर्थन करने के लिए (नामस्थान पीढ़ी के लिए फ़ोल्डर संरचना का एक हिस्सा उपयोग करके)। लेकिन उपरोक्त शॉट के लिए ऊपर की मदद करनी चाहिए।
लोग फ़ाइल/टीएफएस कभी-कभी भ्रमित हो जाते हैं जब लोग resx का नाम बदलते हैं। बस .cs फ़ाइल को हटाएं और इस काम का उपयोग करके एक आकर्षण की तरह इसे पुन: प्रयास करें। – MartijnK
सार्वजनिक रूप से एक्सेस मॉडिफायर सेट करना बेकार ढंग से काम करता है - धन्यवाद। – Mani5556
संसाधनों को सहेजना .resx पर्याप्त है, एक्सेस संशोधक को बदलने की कोई आवश्यकता नहीं है। –