C# 你好,世界[WPF]

C# 你好,世界[WPF],c#,.net,wpf,windows,C#,.net,Wpf,Windows,我正在尝试在WPF中创建我的hello world windows应用程序 我应该做什么来运行此窗口 Class1.xaml 我已经创建了一个空白sln并添加了这三个文件。我还添加了WindowsBase、PresentationBase、PresentationFramework引用 但应用程序没有运行 问题出在哪里?使用WPF项目?创建一个新的WPF应用程序将是一个不错的选择,但我可能知道一种解决方法 您的项目文件当前可能有如下部分: <Page Include="App.xaml"&

我正在尝试在WPF中创建我的hello world windows应用程序

我应该做什么来运行此窗口

Class1.xaml 我已经创建了一个空白sln并添加了这三个文件。我还添加了WindowsBase、PresentationBase、PresentationFramework引用

但应用程序没有运行


问题出在哪里?

使用WPF项目?

创建一个新的WPF应用程序将是一个不错的选择,但我可能知道一种解决方法

您的项目文件当前可能有如下部分:

<Page Include="App.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>
[STAThread]
public static void Main()
{
    App app = new App();
    app.StartupUri = new System.Uri("/Project1;component/Class1.xaml", System.UriKind.Relative);                     
    app.Run();
}
这样,您的项目应该知道在运行应用程序时实际启动什么


我从中提取的解决方案是VS2010解决方案,但我认为在VS2008中也是如此。

如果要使用
Main
方法,则将忽略App.xaml文件

App.xaml仅在将其生成操作设置为
ApplicationDefinition
时使用。当您执行此操作时,您会注意到出现了一个编译器错误,因为您的程序中有两个入口点——因此您必须丢失
Main
方法

如果要保留
Main
方法,可以。不要更改App.xaml上的构建操作(实际上,我认为您可以删除它),然后执行以下操作:

<Page Include="App.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>
[STAThread]
public static void Main()
{
    App app = new App();
    app.StartupUri = new System.Uri("/Project1;component/Class1.xaml", System.UriKind.Relative);                     
    app.Run();
}
/Project1
替换为您的命名空间

<ApplicationDefinition Include="App.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</ApplicationDefinition>
[STAThread]
public static void Main()
{
    App app = new App();
    app.StartupUri = new System.Uri("/Project1;component/Class1.xaml", System.UriKind.Relative);                     
    app.Run();
}