C# Roslyn将内容嵌入到程序集中
是否可以使用roslyn将C# Roslyn将内容嵌入到程序集中,c#,.net,resources,roslyn,C#,.net,Resources,Roslyn,是否可以使用roslyn将内容嵌入程序集?嵌入资源效果很好,但不知道如何添加内容。我添加了如下资源: foreach (string file in Directory.GetFiles(inputPath).Where(item => item.EndsWith(".xaml"))) { var resourceDescription = new ResourceDescription( Path.GetFileName(file.ToLower(
内容嵌入程序集?嵌入资源效果很好,但不知道如何添加内容。我添加了如下资源:
foreach (string file in Directory.GetFiles(inputPath).Where(item => item.EndsWith(".xaml")))
{
var resourceDescription = new ResourceDescription(
Path.GetFileName(file.ToLower()),
() => File.OpenRead(file.ToLower()),
true);
resourceDescriptions.Add(resourceDescription);
}
然后在发射时通过:
EmitResult result = compilation.Emit(ms, manifestResources: resourceDescriptions.ToArray());
但我现在不想添加其他类型的构建操作
编辑
必须可以使用System.Windows.Application.LoadComponent(这是resourceLocater)加载内容代码>
编辑2
好的,出于测试目的,我添加了.xaml和.baml,但我仍然得到了消息,
找不到testusercontrol.xaml
下面是我添加这些文件的方式:
List<ResourceDescription> resourceDescriptions = new List<ResourceDescription>();
foreach (string file in Directory.GetFiles(outputPath).Where(item => item.EndsWith(".baml")))
{
var resourceDescription = new ResourceDescription(
string.Format("{0}.g.{1}", RootNamespace, Path.GetFileName(file.ToLower())),
Path.GetFileName(file.ToLower()),
() => File.OpenRead(file.ToLower()),
true);
resourceDescriptions.Add(resourceDescription);
}
foreach (string file in Directory.GetFiles(inputPath).Where(item => item.EndsWith(".xaml")))
{
var resourceDescription = new ResourceDescription(
string.Format("{0}.g.{1}", RootNamespace, Path.GetFileName(file.ToLower())),
Path.GetFileName(file.ToLower()),
() => File.OpenRead(file.ToLower()),
true);
resourceDescriptions.Add(resourceDescription);
}
EmitResult result = compilation.Emit(ms, manifestResources: resourceDescriptions.ToArray());
它也可以在ILSpy
中看到:
但找不到testusercontrol.xaml
的消息框仍然存在。我做错了什么
编辑5-解决方案
现在可以了!步骤:
首先:在名称空间和程序集名称下添加资源,在任何其他情况下,roslyn都会制造一些麻烦:
// Add ressource under the namespace AND assembly
var resourceDescription = new ResourceDescription(
string.Format("{0}.g.resources", RootNamespace),
() => File.OpenRead(resourcePath),
true);
resourceDescriptions.Add(resourceDescription);
if (RootNamespace != assemblyName)
{
resourceDescription = new ResourceDescription(
string.Format("{0}.g.resources", assemblyName),
() => File.OpenRead(resourcePath),
true);
resourceDescriptions.Add(resourceDescription);
}
第二:将资源添加为流:
foreach (string file in Directory.GetFiles(outputPath).Where(item => item.EndsWith(".baml")))
{
var fileName = Path.GetFileName(file.ToLower());
var data = File.OpenRead(file);
rsWriter.AddResource(fileName, data, true);
}
第三:快乐
谢谢大家! 要么我完全误解了这个问题,要么你问的是一个正方形三角形
“内容”和“资源”之间的区别在于内容位于程序集之外,而资源则嵌入到程序集中System.Windows.Application.LoadComponent
无法加载资源,它只能加载内容
因此,您所要求的原则上是不可能的。但MS正在将System.Windows.Application.LoadComponent
嵌入到它的.g.cs
文件中。但我从未在一些bin\debug
或release
目录中见过它们。因此,它必须可以从程序集内部加载。是否有其他的构建操作
像页面
我可以与Roslyn
一起使用?多谢各位!所以,是的,你是对的。这些被嵌入到YourNamespace.g.resources中,它本身就是一个包含所有xaml编译目的的资源,感谢您的快速响应:)我编辑了我的初始帖子,并添加了更多细节。我觉得我使用了一个错误的资源名称,但不知道它应该是什么样子。你在那里看到一个失败吗?出于测试目的,我同时添加了xaml和bamlYep,这是一个很好的进展。我认为您需要创建.resource文件,如我上面评论的第一个链接所述。您需要将baml放入该文件中,最后以与现在相同的方式将该文件添加到资源中,名称为YourNamespace.g.resources.Ok,添加资源效果很好,但错误仍然存在。我现在做错了什么。谢谢你的耐心:)(我编辑了我的帖子)你为什么在你的问题中写下你的答案。回答你的问题
foreach (string file in Directory.GetFiles(outputPath).Where(item => item.EndsWith(".baml")))
{
var fileName = Path.GetFileName(file.ToLower());
var data = File.OpenRead(file);
rsWriter.AddResource(fileName, data, true);
}