简体   繁体   English

在Windows Forms C#中查看WPF表单

[英]Viewing a form of WPF in Windows Forms C#

I wrote this method did not work 我写的这个方法不起作用

UserControl us = new UserControl();
us.Show();

Your control has to be located in a window 您的控件必须位于一个窗口中

Window  Window = new Window();
// window has a single content
// here it is usercontrol1
// to have many controls, use an intermediary like Grid or Canvas or any Panel derived class
window.Content = usercontrol1;

The Window has to be opened. 该窗口必须打开。

// modeless (non blocking) opening
window.Show();

or 要么

// modal (blocking) opening
window.Showdialog();

Regards 问候

You can't show a UserControl . 您无法显示UserControl Change your UserControl to a Window . 将您的UserControl更改为Window

XAML: XAML:

<Window x:Class="WindowsFormsApplication1.MyWindow"

instead of 代替

<UserControl x:Class="WindowsFormsApplication1.UserControl1"

and in your code-behind, change 然后在您的代码背后,进行更改

public partial class UserControl1 : UserControl

to

public partial class MyWindow: Window

Now you can call new MyWindow().Show(); 现在您可以调用new MyWindow().Show(); . The major benefit is that you are not overburdening the application by adding a Windows Form dialog and an ElementHost and a UserControl inside it. 主要的好处是,您不会因在其中添加Windows窗体对话框以及ElementHost和UserControl而给应用程序增加负担。

This way you can also access the children of the UserControl/Window from the calling Windows Form class. 这样,您还可以从调用Windows Form类访问UserControl / Window的子级。

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

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