I have existing winforms application. In this application one form is called in separate thread:
我有winforms应用程序。在此应用程序中,一个表单在单独的线程中调用:
var newDialogThread = new Thread(new ThreadStart(ShowMyForm));
newDialogThread .Name = "CustomerViewThread";
newDialogThread .IsBackground = true;
newDialogThread .ApartmentState = ApartmentState.STA;
newDialogThread .CurrentCulture = Config.CustomerCulture;
public void ShowMyForm()
{
using(var myForm = new MyForm())
{
Application.Run(myForm );
}
}
Now customer wants to use WPF form instead of this. Application will stay Winforms based.
现在客户想要使用WPF表单而不是这个。应用程序将保持Winforms为基础。
How properly to refactor this part of code so my WPF form will be called instead of Winforms?
如何正确地重构这部分代码,以便调用我的WPF表单而不是Winforms?
public void ShowMyForm()
{
using(var myWpfForm = new MyWpfForm())
{
// Analog of Application Run?
//Application.Run(myForm );
}
}
P.s. I think that its possible to create pure WPF window without hosting it to Winforms. And this is main case.
附:我认为可以创建纯WPF窗口而无需将其托管到Winforms。这是主要案例。
3 个解决方案
#1
2
It sounds like you need to edit your (WinForms
) MyForm
control to add an ElementHost
control into it, so that it can host your WPF control:
听起来你需要编辑你的(WinForms)MyForm控件来添加一个ElementHost控件,以便它可以托管你的WPF控件:
MyWpfForm myWpfForm = new MyWpfForm();
ElementHost elementHost = new ElementHost();
elementHost.Child = myWpfForm;
this.Controls.Add(elementHost);
In this way, you can leave your ShowMyForm
method unchanged.
这样,您可以保持ShowMyForm方法不变。
#2
1
This article explains how to do it. But basically:
本文介绍了如何执行此操作。但基本上:
1) Create/Add a new project of type "WPF Custom Control Library"
1)创建/添加“WPF自定义控件库”类型的新项目
2) Add a new Item of type "Window (WPF)"
2)添加一个类型为“Window(WPF)”的新项
3) Do your thing with the WPF Window
3)使用WPF窗口做你的事情
4) From your WinForms app, create and open the WPF Window:
4)从WinForms应用程序中,创建并打开WPF窗口:
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
//Place the following code where you want to open the WPF window
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
#3
0
Found answer in related thread: How do I create and show WPF windows on separate threads?
在相关主题中找到答案:如何在不同的线程上创建和显示WPF窗口?
private void NewWindowHandler(object sender, RoutedEventArgs e)
{
Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
}
private void ThreadStartingPoint()
{
Window1 tempWindow = new Window1();
tempWindow.Show();
System.Windows.Threading.Dispatcher.Run();
}
http://msdn.microsoft.com/en-us/library/ms741870.aspx
http://msdn.microsoft.com/en-us/library/ms741870.aspx
#1
2
It sounds like you need to edit your (WinForms
) MyForm
control to add an ElementHost
control into it, so that it can host your WPF control:
听起来你需要编辑你的(WinForms)MyForm控件来添加一个ElementHost控件,以便它可以托管你的WPF控件:
MyWpfForm myWpfForm = new MyWpfForm();
ElementHost elementHost = new ElementHost();
elementHost.Child = myWpfForm;
this.Controls.Add(elementHost);
In this way, you can leave your ShowMyForm
method unchanged.
这样,您可以保持ShowMyForm方法不变。
#2
1
This article explains how to do it. But basically:
本文介绍了如何执行此操作。但基本上:
1) Create/Add a new project of type "WPF Custom Control Library"
1)创建/添加“WPF自定义控件库”类型的新项目
2) Add a new Item of type "Window (WPF)"
2)添加一个类型为“Window(WPF)”的新项
3) Do your thing with the WPF Window
3)使用WPF窗口做你的事情
4) From your WinForms app, create and open the WPF Window:
4)从WinForms应用程序中,创建并打开WPF窗口:
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
//Place the following code where you want to open the WPF window
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
#3
0
Found answer in related thread: How do I create and show WPF windows on separate threads?
在相关主题中找到答案:如何在不同的线程上创建和显示WPF窗口?
private void NewWindowHandler(object sender, RoutedEventArgs e)
{
Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
}
private void ThreadStartingPoint()
{
Window1 tempWindow = new Window1();
tempWindow.Show();
System.Windows.Threading.Dispatcher.Run();
}
http://msdn.microsoft.com/en-us/library/ms741870.aspx
http://msdn.microsoft.com/en-us/library/ms741870.aspx