如何在启动程序后立即运行方法?

时间:2014-05-26 15:27:21

标签: c# wpf xaml

我正在使用C#WPF,但在启动.exe文件后,我无法弄清楚如何启动方法。我已经制作了一个控制台应用程序,我不希望看到任何窗体/窗口。

我摆脱了整个MainWindow()方法,我还在我的Main()方法中添加了我想要调用的方法。我从App.xaml中删除了StartupUri="MainWindow.xaml"行,我希望软件在启动时运行Main()方法,但它会继续从MainWindow()调用MainWindow.cs方法}类。我知道,因为当我放回我的MainWindow()方法时,它执行了该方法中的所有内容。

我尝试的第一件事是将我的方法调用MainWindow()方法,但它保持循环,我不希望它。我想解决这个问题的方法是无穷无尽的Thread.Sleep(),但这似乎是一件非常愚蠢的事情。

以下是我的Main()方法:

    /// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public static void Main() {
          System.Windows.MessageBox.Show("it's working"); //shows this message it this method is called
    }

以下是整个App.xaml

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

    </Application.Resources>
</Application>

1 个答案:

答案 0 :(得分:1)

很好,你找到了它。您可以将此评论标记为答案,然后......:)

如果你真的创建了一个控制台应用程序,你就不会有任何XAML ...可能你没有创建一个Console-App,而是一个WPF应用程序。尝试在VS中查找Console-App