C#WPF应用程序首先打开常规Windows窗体
本文关键字:常规 Windows 窗体 应用程序 C#WPF | 更新日期: 2023-09-27 18:24:49
我有一个WPF应用程序,默认情况下希望首先启动一个常规的Windows窗体。基本上,这将是一个选项面板。
edit:为了澄清,我希望Windows窗体是唯一一个自动打开的窗体。稍后我将展示WPF表格。
我尝试修改App.xaml:
<Application x:Class="The_Name_Of_My.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Form1.cs">
<Application.Resources>
我得到一个错误:"找不到资源'form1.cs'。"我只是在这里旋转轮子,试图做一些愚蠢的事情,而我应该在WPF中制作选项面板吗?
只需在WPF窗体的/Viewmodels构造函数中实例化Windows窗体并调用ShowDialog:
namespace WpfApplication11
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var form1 = new Form1();
form1.ShowDialog();
Close();
}
}
}
我在默认情况下加载Windows_Form_Application时遇到了同样的问题。但答案如下。。。只需在您的项目中添加Windows_Form,并将以下代码替换为MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
//Make WPF form unvisible
this.AllowsTransparency = true;
this.Background = null;
this.WindowStyle = WindowStyle.None;
//Run windows from
Form1 u = new Form1();
u.Show();
//Set close handle to form
u.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
}
private void Form1_FormClosing(object sender, EventArgs e)
{
//close WPF form if windows form being close
this.Close();
}
就是这样…