wpf 有多个入口点定义错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24547290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Has more than one entry point defined error
提问by jason
I have the following code :
我有以下代码:
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MyWindow : Window
{
public MyWindow()
{
Width = 300; Height = 200; Title = "My Program Window";
Content = "This application handles the Startup event.";
}
}
class Program
{
static void App_Startup(object sender, StartupEventArgs args)
{
MessageBox.Show("The application is starting", "Starting Message");
}
[STAThread]
static void Main()
{
MyWindow win = new MyWindow();
Application app = new Application();
app.Startup += App_Startup;
app.Run(win);
}
}
}
When I run this code, I get the following error:
当我运行此代码时,出现以下错误:
Error 1 Program 'c:...\WpfApplication2\WpfApplication2\obj\Debug\WpfApplication2.exe' has more than one entry point defined: 'WpfApplication2.Program.Main()'. Compile with /main to specify the type that contains the entry point.
错误 1 程序 'c:...\WpfApplication2\WpfApplication2\obj\Debug\WpfApplication2.exe' 定义了多个入口点:'WpfApplication2.Program.Main()'。使用 /main 编译以指定包含入口点的类型。
There is no "Program"file on my code as far as I see. I don't know how to fix this. Can you tell me how to fix? Thanks.
据我所知,我的代码中没有“程序”文件。我不知道如何解决这个问题。你能告诉我如何解决吗?谢谢。
回答by pushpraj
You have two primary option to implement on start activities
您有两个主要选项可以在开始活动时实施
Event handlers
事件处理程序
App.xaml
应用程序.xaml
<Application x:Class="CSharpWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="Application_Startup">
define Startup="Application_Startup"and handle in App.xaml.cs
Startup="Application_Startup"在 App.xaml.cs 中定义和处理
private void Application_Startup(object sender, StartupEventArgs e)
{
//on start stuff here
}
Override method
覆盖方法
App.xaml.cs
应用程序.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
//on start stuff here
base.OnStartup(e);
//or here, where you find it more appropriate
}
}
回答by ALi
In order to solve this error, Don't change the name of cs file when creating a new code.
为了解决这个错误,创建新代码时不要更改cs文件的名称。
If you want a new name for your cs file then you have to delete everything in folder where your project is i.e., the fragments of file(bin, obj folders,etc)
如果您想为您的 cs 文件取一个新名称,那么您必须删除项目所在文件夹中的所有内容,即文件片段(bin、obj 文件夹等)

