0

我在 Microsoft Expression Blend 4 中创建了一个准系统 WPF 项目。

然后我在 Visual Studio 2012 中打开了该项目,并向该项目添加了一个简单的类。

我将应用程序属性设置为使用此类作为启动对象。

我创建了一个新的主窗口,然后对该对象使用了 show 函数。

窗口会弹出一毫秒,然后关闭。

如何调用 MainWindow 使其保持打开状态?

Class1.cs

//This is the Class I created
using System;
using System.Collections.Generic;

namespace WpfApplication5
{
    static class Class1
    {
        [STAThread]
        static void Main()
        {
            MainWindow winMain = new MainWindow();
            winMain.Show();
        }

    }
}

主窗口.xaml.cs

//This is the Mainwindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication5
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

        }
    }
}

主窗口.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication5.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot"/>
</Window>

应用程序.xaml

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

    </Application.Resources>
</Application>

应用程序.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApplication5
{
public partial class App : Application
    {
    }
}
4

1 回答 1

1

您为应用程序设置了两个入口点。一个在 App.xaml 中,另一个在 Class1.cs 中。所以最好从 Class1.cs 中删除下面的代码块

[STAThread] 
static void Main() 
{
  MainWindow winMain = new MainWindow();
  winMain.Show();
}
于 2013-07-15T05:29:25.147 回答