'InitializeComponent' 在当前上下文中不存在

'InitializeComponent' does not exist in the current context

我正在使用 Roslyn 来解析一个非常基本的 WPF 解决方案。我设置了诊断并发现了以下错误:

MainWindow.xaml.cs(25,13): error CS0103: The name 'InitializeComponent' does not exist in the current context

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

知道如何解决这个问题吗?

更新

所以我添加了一个主要方法,现在我收到以下错误:

App.xaml.cs(20,17): error CS1061: 'App' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)

MainWindow.xaml.cs(25,13): error CS0103: The name 'InitializeComponent' does not exist in the current context

这里是主要方法

public partial class App : Application
{
    [STAThread]
    public static void Main()
    {
        var app = new App();
        app.InitializeComponent();
        app.Run();
    }
}

如果您使用类似于以下的代码加载此解决方案:

var ws = MSBuildWorkspace.Create();
var solution = await ws.OpenSolutionAsync(path);

那么问题是您没有分析应该从您的 .xaml 文件生成的代码。这可以通过将上面的代码更改为:

来解决
var properties = new Dictionary<string, string> { ["DesignTimeBuild"] = "true" };
var ws = MSBuildWorkspace.Create(properties);
var solution = await ws.OpenSolutionAsync(path);

有关详细信息,另请参阅 https://github.com/dotnet/roslyn/issues/2779