Compiling a XAML.cs file.
-
Donnerstag, 26. April 2012 12:58
Hi,
I'm starting to make a some progress using Roslyn. Thanks to Shyam!
Can anyone point me to an example of compiling a XAML based type. ie MyControl.xaml.cs?
(I'm a bit strapped for time but when I get more time I'll document my findings better - ie this evening!)
regards
Richard
- Bearbeitet Richard Matthew Hill Donnerstag, 26. April 2012 14:31 more info
Alle Antworten
-
Donnerstag, 26. April 2012 23:15
Files with .xaml.cs extension are just normal C# files that contain a partial class, so there shouldn't be a problem doing that using Roslyn.
The problem might be getting the other part, that is generated from the .xaml file. But if you don't need that, I don't think you need any special considerations for .xaml.cs files.
Did you encounter some specific problems?
-
Freitag, 27. April 2012 01:18Besitzer
Hi Richard - svick is correct. Xaml.cs files shouldn't be any special by themselves but you would need to include 'generated' files (MyControl.g.cs and App.g.cs files under the obj folder) into your 'Compilation' / 'IProject' so that the symbols for the members declared in these files are available to your Compilation... The Workspace API won't add these generated files automatically when you say Solution.Load("<Path>") / Workspace.LoadSolution("<Path>") / Workspace.LoadStandaloneProject("<Path>").
I just tried the following experiment against a simple WPF Windows Application project that I created using Visual Studio and the Compilation seems to end up in a good state after I load the .g.cs files into the project.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using Roslyn.Compilers.Common; using Roslyn.Services; class Program { static void Main() { var folder = @"<Update with path to folder containing .sln file for a simple Wpf Application>"; var slnFile = Path.Combine(folder, @"WpfApplication1.sln"); var solution = Solution.Load(slnFile, "Debug", "x86"); // Since my Wpf Application project targets x86. // There is a known bug around 'x86' / 'AnyCPU' - // see http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/1a2fee87-6e70-4b5e-b9c0-1e8b9e992302. var project = solution.Projects.Single(); // Since I know that the solution only contains one project. var projectId = project.Id; PrintDiagnostics("Initial Errors", project.GetCompilation().GetDiagnostics()); var objDebugFolder = Path.Combine(folder, @"obj\x86\Debug"); foreach (var csharpFile in Directory.GetFiles(objDebugFolder, "*.g.cs", SearchOption.AllDirectories)) { DocumentId docId; solution = solution.AddDocument(projectId, csharpFile, out docId); } // Projects and solutions are immutable - we need to re-get the project since we // changed it by adding files to it above. project = solution.GetProject(projectId); PrintDiagnostics("Errors after including *.g.cs files", project.GetCompilation().GetDiagnostics()); // Lets try to get the TypeSymbol for the MainWindow of the Wpf Application. PrintSymbol("Symbol for MainWindow", project.GetCompilation().GetTypeByMetadataName("WpfApplication1.MainWindow")); } private static void PrintDiagnostics(string prefix, IEnumerable<IDiagnostic> diagnostics) { Console.WriteLine("\n" + prefix + ":"); if (diagnostics.Any()) { foreach (var diagnostic in diagnostics) { Console.WriteLine(diagnostic.ToString()); } } else { Console.WriteLine("No Errors"); } } private static void PrintSymbol(string prefix, ISymbol symbol) { Console.WriteLine("\n" + prefix + ":"); if (symbol == null) { Console.WriteLine(prefix + "null"); } else { Console.WriteLine(symbol.ToDisplayString() + " (" + symbol.GetType().Name + ", " + symbol.Kind + ")"); } } }
Note: Change the paths / filenames in the above code to point to a simple Wpf Application project.
I wouldn't expect Roslyn to compile 'every' Wpf project without errors yet :) because the Roslyn CTP does not support all language features yet. You will probably run into other compiler errors (i.e. Diagnostics) if you try this against Wpf projects that use any unsupported language features. But even if there are some Diagnostics in the Compilation, you should still be able to get symbols for things / do semantic analysis...
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
- Bearbeitet Shyam NamboodiripadMicrosoft Employee, Owner Freitag, 27. April 2012 01:23
- Als Antwort markiert Richard Matthew Hill Montag, 30. April 2012 11:16

