I'm looking to use xaml file to make auto generate file (*.g.cs file)
I find in the roslyn solution, in MSBuidWorkspaceTests.cs, this method :
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 it, tests fail..
Do you know how using xaml file with roslyn? How can I generate *.g.cs file? In this example, they had xaml file to the solution, but xaml file aren't in documents property so where are they?
Roslyn itself can't, but you can use MSBuild to do that (with the XamlBuildTask
assembly).
Here's a code snippet to illustrate:
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();
The 'References' property is a list of paths of all the required assemblies. I added them as references to the project, so that I could get their location via the typeof(ApplicationGesture).Assembly.Location
construct.
Note that the required assemblies are:
Finally, here's the definition of the XamlItem
class (just a dummy implementation of 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; } }
}
See the MSDN reference for PartialClassGenerationTask
for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With