【发布时间】:2017-07-29 20:57:23
【问题描述】:
是否可以使用 roslyn 将 content 嵌入到程序集中?嵌入资源效果很好,但不知道如何添加内容。我添加这样的资源:
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());
但我现在不添加其他类型的Build Actions。
编辑
content 必须可以使用 System.Windows.Application.LoadComponent(this, 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());
我是否使用了错误的ressourceName(使用编辑 3,应该是这样)?
编辑 3
我想我出了什么问题。程序集内部没有创建资源目录。它应该是这样的:
这就是它目前的样子:
但是如何使用 roslyn 创建这些目录?
编辑 4
好的,使用以下代码嵌入资源:
string resourcePath = string.Format("{0}{1}.g.resources", outputPath, RootNamespace);
ResourceWriter rsWriter = new ResourceWriter(resourcePath);
foreach (string file in Directory.GetFiles(outputPath).Where(item => item.EndsWith(".baml")))
{
var fileName = Path.GetFileName(file.ToLower());
var data = File.ReadAllBytes(file);
rsWriter.AddResource(fileName, data);
}
foreach (string file in Directory.GetFiles(inputPath).Where(item => item.EndsWith(".xaml")))
{
var fileName = Path.GetFileName(file.ToLower());
var data = File.ReadAllBytes(file);
rsWriter.AddResource(fileName, data);
}
rsWriter.Generate();
rsWriter.Close();
var resourceDescription = new ResourceDescription(
string.Format("{0}.g.resources", RootNamespace),
() => File.OpenRead(resourcePath),
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);
}
第三:快乐
谢谢大家!
【问题讨论】:
-
为什么你在你的问题中写下你的答案。回答你的问题