2013-07-15 34 views
0

我在Microsoft Expression Blend创建的准系统WPF项目4WPF XAML主窗口dissappears其所谓的使用放映功能

然后我在Visual Studio 2012年开业的项目,并增加了一个简单的类的项目。

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

我创建了一个新的主窗口,然后使用该对象的显示功能。

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

如何调用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(); 
     } 

    } 
} 

MainWindow.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(); 

     } 
    } 
} 

MainWindow.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> 

个的App.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> 

App.xaml.cs

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

namespace WpfApplication5 
{ 
public partial class App : Application 
    { 
    } 
} 
+0

为什么你需要那个Class1.cs?您已经通过设置StartupUri在App.xaml中定义了应用程序的入口点。 – Nitesh

+0

由于设置了2个入口点,我不知道在编译项目时没有得到任何异常。所以最好删除 [STAThread] static void Main() { MainWindow winMain = new MainWindow(); winMain.Show(); } – Nitesh

+0

我使用Class1.cs作为最简单的例子,Class1最终将包含核心程序代码,并向其他类发出调用,并且我将需要显示几个窗口,具体取决于代码中发生的情况。因此需要/希望通过代码调用xaml窗口。我没有收到任何例外,至今没有人回答过问题。我尝试从App.xaml中删除Startupuri调用,但这也没有奏效。谢谢! –

回答

1

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

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