28

.resxリソースファイルを生成するプログラムを入手しました。これらのリソースファイルは、リソースファイルを生成するプロジェクトと同じソリューションではない他のプロジェクトで使用されます。

designer.csresxresourcereaderを使用せずにリソースに直接アクセスできるように、リソースファイルからファイルを生成できるかどうか疑問に思います。

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

ファイルをVisualStudioプロジェクトに追加する場合は、ファイルのCustom Toolプロパティをに設定する必要があり.resxます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";
}

上記のコードを少しフラットにしました。実際のソリューションでは、単純なファイル名の代わりに各画像にカスタムクラスを使用して、異なるサブフォルダーで同じファイル名をサポートします(名前空間のフォルダー構造の一部を使用することにより)世代)。しかし、最初のショットでは、上記が役立つはずです。

于 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はDesignerファイルを再生成します。

于 2017-04-04T14:27:47.977 に答える