使用 roslyn 编译 Xaml 以获取 *.g.cs 文件

Pas*_*TeK 5 c# wpf xaml roslyn

我正在寻找使用 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"));
    }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时,测试失败了..

你知道如何在 roslyn 中使用 xaml 文件吗?如何生成 *.g.cs 文件?在这个例子中,他们有 xaml 文件到解决方案,但 xaml 文件不在文档属性中,那么它们在哪里?

Mét*_*ule 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();
Run Code Online (Sandbox Code Playgroud)

“引用”属性是所有必需程序集的路径列表。我将它们添加为项目的引用,以便我可以通过typeof(ApplicationGesture).Assembly.Location构造获取它们的位置。请注意,所需的组件是:

  • 系统
  • 系统.Xaml
  • WindowsBase
  • 演示框架
  • 演示核心

最后,这是该类的定义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; } }
}
Run Code Online (Sandbox Code Playgroud)

PartialClassGenerationTask有关详细信息,请参阅 MSDN 参考