Compile Xaml with roslyn to get a * .g.cs file

I want to use the xaml file to create an automatic file generation file (* .g.cs)

I found this method in the roslyn solution in 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"));
    }

But when I try, the tests fail.

Do you know how to use xaml file with roslyn? How can I generate a * .g.cs file? In this example, they had an xaml file to solve, but the xaml file does not have a document property, so where are they?

+4
source share
1 answer

Roslyn itself cannot, but you can use MSBuild for this (using the build XamlBuildTask). Here is a code snippet illustrating:

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();

"" - . , typeof(ApplicationGesture).Assembly.Location. , :

  • System.Xaml
  • WindowsBase
  • PresentationFramework
  • PresentationCore

, 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; } }
}

. MSDN PartialClassGenerationTask

+2

Source: https://habr.com/ru/post/1544067/


All Articles