WPF Xaml MainWindow在使用Show函数调用后将不显示

本文关键字:函数调用 显示 Show Xaml MainWindow WPF | 更新日期: 2023-09-27 18:22:30

我在Microsoft Expression Blend 4中创建了一个基本的WPF项目。

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

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

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

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

如何调用主窗口,使其保持打开状态?

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
    {
    }
}

WPF Xaml MainWindow在使用Show函数调用后将不显示

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

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