Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF中没有Main()?_C#_Wpf_C# 4.0_Main - Fatal编程技术网

C# WPF中没有Main()?

C# WPF中没有Main()?,c#,wpf,c#-4.0,main,C#,Wpf,C# 4.0,Main,谈到编程,我是一个初学者,但我确信其中一个普遍规则是程序以Main()开头。我在创建WPF项目时没有看到一个。Main()在WPF中的名称是否不同?Main()是在编译过程中生成的。您可以在App.g.cs(在obj/{Debug,Release}文件夹中)中找到它。它是在构建过程中生成的,但您可以提供自己的(根据需要在项目属性中消除歧义)。在obj/debug中查找应用程序文件;我(由“C#2010快车”提供)有App.g.I.cs和: namespace WpfApplication1 {

谈到编程,我是一个初学者,但我确信其中一个普遍规则是程序以Main()开头。我在创建WPF项目时没有看到一个。Main()在WPF中的名称是否不同?

Main()
是在编译过程中生成的。您可以在
App.g.cs
(在
obj/{Debug,Release}
文件夹中)中找到它。

它是在构建过程中生成的,但您可以提供自己的(根据需要在项目属性中消除歧义)。在obj/debug中查找应用程序文件;我(由“C#2010快车”提供)有
App.g.I.cs
和:

namespace WpfApplication1 {


    /// <summary>
    /// App
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public partial class App : System.Windows.Application {

        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void InitializeComponent() {

            #line 4 "..\..\..\App.xaml"
            this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);

            #line default
            #line hidden
        }

        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public static void Main() {
            WpfApplication1.App app = new WpfApplication1.App();
            app.InitializeComponent();
            app.Run();
        }
    }
}
命名空间WpfApplication1{
/// 
///应用程序
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“PresentationBuildTasks”,“4.0.0.0”)]
公共部分类应用程序:System.Windows.Application{
/// 
///初始化组件
/// 
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent(){
#第4行“.\..\..\App.xaml”
this.StartupUri=new System.Uri(“MainWindow.xaml”,System.UriKind.Relative);
#行默认值
#隐藏线
}
/// 
///应用程序入口点。
/// 
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
公共静态void Main(){
WpfApplication1.App App=新的WpfApplication1.App();
app.InitializeComponent();
app.Run();
}
}
}
main()
是应用程序的标准入口点,但所有应用程序的结构都是这样的。在XAML项目中,App.XAML文件指定入口点,其中显示
StartupUri=“MainWindow.XAML”

正如其他人所说,实际的main函数是根据项目中XAML文件的内容生成的。

main()
由CLR和WPF自动提供

C#编译器采用命令行开关
/m
,该开关指定包含
Main()
实现的类型。按照惯例,如果没有显式指定启动对象,CLR将查找任何具有静态
Main()
方法的类并调用它。(正如@Marc Grasser在评论中指出的那样)

对于WPF,在构建
App.xaml
时会自动生成
Main()
,并指定/m开关以使C#编译器将该类用作入口点。但是,如果查看项目属性,您会发现有一个用于选择启动对象的设置。因此,如果需要,您可以提供自己的类来实现
Main()

请注意,这将让您负责创建
应用程序
实例并调用其
Run()
方法,以确保WPF基础结构正确启动。

Main()方法是自动创建的。 如果您想提供自己的,您必须(在VS2013、VS2017和VS2019中测试):

  • 在解决方案资源管理器中右键单击App.xaml,选择属性
  • 将“构建操作”更改为“页面”(初始值为“ApplicationDefinition”)
然后只需向App.xaml.cs添加一个Main()方法。可能是这样的:

[STAThread]
public static void Main()
{
    var application = new App();
    application.InitializeComponent();
    application.Run();
}

我将无法加载到另一个没有主窗口的项目中的文件复制到一个新项目中,并出现此错误

对我来说,需要对Andreas Kahler采取相反的方法来修复:


制作了一个窗口文件并将启动uri设置到此文件后,我将页面切换到ApplicationDefinition of App.xaml“Build Action”属性。

如果删除了默认的App.xaml和MinWindow.xaml,最好编辑.csproj 手动添加App.xaml后,您的.csproj将:

<Page Include ="App.xaml">
       <DependentUpon>MSBuild:Compile</DependentUpon>
       <SubType>Code</SubType>
</Page>

MSBuild:编译
代码

将此更改为:

<ApplicationDefinition Include="App.xaml">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
</ApplicationDefinition>

MSBuild:编译
设计师

实际上,没有
/m
它不关心类型的名称;如果不明确,它只会尝试找到任何合适的
Main
方法,如果找到0个或多个方法,它就会抱怨。例如,语言规范(§1.1)中的“Hello World”示例使用
Hello
作为类型名。学究:说
main()
由CLR(运行时)提供并不准确,它实际上是由编译器生成的。添加my+1是为了提醒观众他们必须添加到自己的应用程序实例中(即
MyApp.App=new MyApp.App();
),并在其上调用
.Run()
,就像前面的
Main()
一样。好电话。此外,还必须添加
app.InitializeComponent()
app.Run()
之前,幸运的是,原始的
InitializeComponent()
方法仍然存在(见app.g.cs,因此无需重新添加该方法!)。您可以通过覆盖app.xaml.cs中的OnStartup来获得等效的功能。StartupEventArgs.Args包含命令行参数。@傻瓜,不,您不能,请参见问题。
App.g.cs
中也存在同样的情况。但是我尝试添加到
Main()
,每次我重建我的项目时,它都会恢复到您现在的状态。试图在另一个类中创建我自己的类,但Project Properties只找到MyProject.App,而不是另一个类,因此无法重定向它。或者,在visual studio中打开
App.xaml.cs
。使用导航栏>方法下拉列表>选择
Main
(灰显)。这将带您进入
App.g.i.cs
[STAThread]设置应用程序的COM线程模型。通常,您只需将其设置为STA,而不必担心它的确切功能。如果您感兴趣,请查看不要这样做!这将永久中断在设计器中为整个项目在样式中的App.xaml中定义的静态资源。在尝试单击Build Action中的页面后,App.xaml将消失您使用的是哪种IDE和项目类型@user3625699?@An