我希望使用xaml文件制作自动生成文件(* .g.cs文件)
我在roslyn解决方案中找到了MSBuidWorkspaceTests.cs中的这个方法:
public void TestOpenProjectAsyncWithXaml()
{
CreateFiles(GetSimpleCSharpSolutionFiles()
.WithFile(@"CSharpProject\CSharpProject.csproj", GetResourceText("CSharpProject_CSharpProject_WithXaml.csproj"))
.WithFile(@"CSharpProject\App.xaml", GetResourceText("CSharpProject_App.xaml"))
.WithFile(@"CSharpProject\App.xaml.cs", GetResourceText("CSharpProject_App.xaml.cs"))
.WithFile(@"CSharpProject\MainWindow.xaml", GetResourceText("CSharpProject_MainWindow.xaml"))
.WithFile(@"CSharpProject\MainWindow.xaml.cs", GetResourceText("CSharpProject_MainWindow.xaml.cs")));
var project = MSBuildWorkspace.Create().OpenProjectAsync(GetSolutionFileName(@"CSharpProject\CSharpProject.csproj")).Result;
var documents = project.Documents.ToList();
// AssemblyInfo.cs, App.xaml.cs, MainWindow.xaml.cs, App.g.cs, MainWindow.g.cs, + unusual AssemblyAttributes.cs
Assert.Equal(6, documents.Count);
// both xaml code behind files are documents
Assert.Equal(true, documents.Contains(d => d.Name == "App.xaml.cs"));
Assert.Equal(true, documents.Contains(d => d.Name == "MainWindow.xaml.cs"));
// prove no xaml files are documents
Assert.Equal(false, documents.Contains(d => d.Name.EndsWith(".xaml")));
// prove that generated source files for xaml files are included in documents list
Assert.Equal(true, documents.Contains(d => d.Name == "App.g.cs"));
Assert.Equal(true, documents.Contains(d => d.Name == "MainWindow.g.cs"));
}
但是当我尝试它时,测试失败了..
你知道如何在roslyn中使用xaml文件吗?如何生成* .g.cs文件? 在这个例子中,他们有xaml文件到解决方案,但xaml文件不在文档属性中,那么它们在哪里?
答案 0 :(得分:2)
Roslyn本身不可以,但您可以使用MSBuild来执行此操作(使用XamlBuildTask
程序集)。
这是一个代码片段来说明:
var msbuildTask = new PartialClassGenerationTask()
{
ApplicationMarkup = new[] { new XamlItem(@"c:\path\to\your\xaml") },
AssemblyName = "AssemblyName",
BuildTaskPath = typeof(PartialClassGenerationTask).Assembly.Location,
Language = "cs", // use "vb" to generate Visual Basic code
OutputPath = @"C:\temp",
References = new[]
{
new XamlItem(typeof(Uri).Assembly.Location),
new XamlItem(typeof(XamlLanguage).Assembly.Location),
new XamlItem(typeof(System.Windows.Point).Assembly.Location),
new XamlItem(typeof(System.Windows.Application).Assembly.Location),
new XamlItem(typeof(ApplicationGesture).Assembly.Location)
},
RequiresCompilationPass2 = false
};
msbuildTask.Execute();
'参考' property是所有必需程序集的路径列表。我将它们添加为项目的引用,以便我可以通过typeof(ApplicationGesture).Assembly.Location
构造获取它们的位置。
请注意,所需的程序集是:
最后,这里是XamlItem
类的定义(只是ITaskItem
的虚拟实现):
private class XamlItem : ITaskItem
{
public XamlItem(string path)
{
ItemSpec = path;
}
public string ItemSpec { get; set; }
public string GetMetadata(string metadataName) { return ""; }
public void SetMetadata(string metadataName, string metadataValue) {}
public void RemoveMetadata(string metadataName) { }
public void CopyMetadataTo(ITaskItem destinationItem) { }
public IDictionary CloneCustomMetadata() { return null; }
public ICollection MetadataNames { get { return null; } }
public int MetadataCount { get { return 0; } }
}
有关详细信息,请参阅PartialClassGenerationTask
的MSDN参考资料