2013-07-15 67 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(); 
}