简体   繁体   English

使用WinForms窗口启动WPF应用程序

[英]Start WPF application with WinForms window

I have WPF application with some WPF windows. 我有一些WPF窗口的WPF应用程序。 But my main window is WinForms and I need to start program with it. 但是我的主窗口是WinForms ,我需要用它启动程序。 But how can I do this? 但是我该怎么办呢? This is my App.xaml file: 这是我的App.xaml文件:

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

If I try to replace StartupUri="UI/MainWindow.xaml" with StartupUri="UI/Form1.cs" , I get "System.IO.IOException", "Cannot find "ui/form1.cs" . Is it even possible to start my app with WinForms window? 如果我尝试将StartupUri="UI/MainWindow.xaml"替换为StartupUri="UI/Form1.cs"StartupUri="UI/Form1.cs"得到"System.IO.IOException", "Cannot find "ui/form1.cs"甚至可能用WinForms窗口启动我的应用程序?

First go to App.xaml and remove StartupUri="UI/MainWindow.xaml" . 首先转到App.xaml并删除StartupUri="UI/MainWindow.xaml" Than go to App.xaml.cs and override OnStartup method. 比转到App.xaml.cs并重写OnStartup方法。 In there you can instantiate and run your win forms window. 在其中,您可以实例化并运行您的胜利表格窗口。

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    var form = new Form1();
    form.Show();
}

There is a good article from MSDN which provides you an example how to integrate an Windows-Forms element in XAML: https://msdn.microsoft.com/de-de/library/ms742875(v=vs.110).aspx MSDN上有一篇不错的文章,其中提供了一个示例如何在XAML中集成Windows窗体元素: https ://msdn.microsoft.com/de-de/library/ms742875( v= vs.110) .aspx

It should be helpful for your suppose. 它应该对您的假设有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM