如何在WPF应用程序中启动主类?

时间:2013-04-25 12:09:31

标签: c# wpf wpf-controls

我是WPF的新手。在我想要显示欢迎表单之后,我在开发WPF应用程序时遇到了问题,我想在启动一个类作为启动对象。当我试图将main方法放在该类中时,将项目属性启动对象设置为该类。 我收到此错误“调用线程必须是STA,因为许多UI组件都需要这个。”

如何通过将该类的main方法作为启动对象来解决此错误?

2 个答案:

答案 0 :(得分:2)

顶部App.xaml

中的

Exit="App_Exit"
Startup="App_Startup"

在app.xaml.cs

void App_Startup(object sender, StartupEventArgs e)
private void App_Exit(object sender, ExitEventArgs e)

您可以订阅其他事件,如果您真的需要覆盖App作为启动,您需要在Program.cs中定义如下:

public static class Program
    {

        [STAThread]
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Main(string[] args)
        {

在那里的某个地方

App app = new App();
app.InitializeComponent();
app.Run();

答案 1 :(得分:-1)

当您说“欢迎表单”时,您的意思是Window吗?

您是否尝试过设置App.xaml的StartupUri?

<Application x:Class="DemoApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="WelcomeWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>