Open WPF window in WindowsForm APP [duplicate] - c#

This question already has answers here:
How to programmatically create a WPF window in a WinForm application
(2 answers)
Closed 5 years ago.
I added to my WindowsForm app a new WPF window called novoLogin.
After adding it, I added the system.xaml reference....debug fine.
Now I'm trying to open this new window from the existing windowsForm.
novoLogin nl = new novoLogin();
nl.show();
The compiler is giving this error:
Error 1 'WindowsFormsApplication1.novoLogin' does not contain a
definition for 'show' and no extension method 'show' accepting a first
argument of type 'WindowsFormsApplication1.novoLogin' could be found
(are you missing a using directive or an assembly reference?)

This brief article explains how you can achieve this.
If you find yourself in need to open a WPF Window from a WinForms program, this is one way to do it (works for me):
Create/Add a new project of type WPF Custom Control Library
Add a new Item of type Window (WPF)
Do your thing with the WPF Window
From your WinForms app, create and open the WPF Window
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();

Have a look to this: http://www.mobilemotion.eu/?p=1537&lang=en
Summary:
Open the project’s manifest file (the one with the .csproj or .vbproj extension) in any text editor.
The top node usually contains several tags, one for each build configuration and a global one. In the global
node (the one without Condition attribute), search for
the sub-node or create one if it does not
exist. This node should contain two GUIDs:
FAE04EC0-301F-11D3-BF4B-00C04F79EFBC, which stands for a C# project,
and 60dc8134-eba5-43b8-bcc9-bb4bc16c2548 which stands for WPF. The
full line should look as follows:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
(If you’re interested in details, codeproject holds a complete list of
potential project GUIDs:
http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs)
Reload the project in Visual Studio, and open the Add New Item wizard.
Since the project is now officially classified as WPF project, this
wizard should now contain the WPF window option. By the way, since
there is no WinForms project GUID that could be overwritten, this
approach does not harm the existing project components.
I just tried this approach for a VB.NET project and it works!
Using VB.NET obviously you have to edit above lines substituting the GUID from {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} to {F184B08F-C81C-45F6-A57F-5ABD9991F28F}

I wanted to show wpf form in windowForm and there was some resource problem...
(because I used resources..). Finally I used this code in my windowsForm project:
First create a global instance of your app class like this :
WPFTest.App app;
why this is global?
because this class is singleton and you can not create more than one instance in the same AppDomain
Now for example you have a button event to show wpf form. At the button event we have :
private void button1_Click(object sender, EventArgs e)
{
if (System.Windows.Application.Current == null)
{
app = new WPFTest.App()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
app.InitializeComponent();
}
else
{
app = (WPFTest.App)System.Windows.Application.Current;
app.MainWindow = new WPFTest.YourWindow();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(app.MainWindow);
app.MainWindow.Show();
}
}
note : WPFTest is name of your project and YourWindow() is window that you wanna to show

Related

Inherited control cannot be found in form design mode

I am developing a C# Windows Form project with Visual Studio 2019. I have inherited a new control, called NewGroupBox, from System.Windows.Forms.GroupBox, the control is under name space MyProject.NewCtrls and was added through the standard procedure How to: Inherit from Existing Windows Forms Controls. I did not do any change on the code generated except changing public partial class NewGroupBox: Control to public partial class NewGroupBox: GroupBox
In the main frame, I have a GroupBox object called groupbox_Table.
groupBox_Table = new System.Windows.Forms.GroupBox();
The form designer could open normal. However, if I manually changed GroupBox with NewGroupBox
groupBox_Table = new MyProject.NewCtrls.NewGroupBox();
The form designer cannot be opened properly. The error message shows "Could not find type 'MyProject.NewCtrls.NewGroupBox' ...". Of course the type is defined in my project and there is no error when building the project.
How do I fix this problem so that form designer can open normally? Thanks a lot!

Is it possible to add some code in constructor runtime after clicking a button from other project?

I'm developing a simple application. I got two projects in the solution
1 WPF Custom Control Library
2 Windows Forms Application
I'm rendering some 3D visualization on WPF and I got that embedded in the windows form as UserControl. So here is what I need to do in this project.
I've got
public void BuildObjectTest(InsertionPoint insertionPoint, Points point) that visualize one object.
Anyway I want it multiple times from my windows Form application because I need to show several objects. I've got a textbox that when I write "barcode" I've got several object items in a LIST that show some label information and buttons PREVIOUS and NEXT. So I have to think in that way is it possible to add code in the constructor runtime or is there any other way I can do that?
This is how the constructor looks like:
public UserControl1()
{
InitializeComponent();
InsertionPoints insertionPoint=new InsertionPoint(0,50,48);
Points points=new Points(100,50,20);
BuildObjectTest(insertionPoint,points);
VisualizeBoxSides();
}
To update UI in WPF Form, I had to use:
userControl.Dispatcher.Invoke(() =>
{
userControl.BuildObjectTest(insertionPoint,points);
});

WPF Form loses it style after showing from another project

I have a C# application which contains 2 project, and when I set the first project which contains a Styled WPF Form as startup project, it has it's own style and everything is fine. But when I use this code to show that WPF from from a Windows Form Application, the first project's form loses its own style:
Window introForm;
introForm = new Client.MainWindow();
introForm.Show();
I have no idea why it happens
I have found my answer here:
https://stackoverflow.com/a/6042515/7147513
this fixed my problem:
var foo = new Uri("pack://application:,,,/MyProject;component/Resources/Styles.xaml", UriKind.RelativeOrAbsolute);
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });

WPF NotifyIcon problems [duplicate]

This question already has an answer here:
Set image in NotifyIcon control from code behind or in XAML
(1 answer)
Closed 9 years ago.
Hi i have some parser error when im trying to add icon to NotifyIcon. This one works fine:
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("C:\\Users\\Daniel\\Documents\\Visual Studio
2012\\Projects\\Pies\\Pies\\main.ico");
But when I'm trying to do it in this way:
ni.Icon = new System.Drawing.Icon("main.ico");
It returns some strange ParserError. I added main.ico to the project. When im adding images in xaml I'm just using "/image/image.jpg" but this don't want to work in this way. Do yuo know why ?
Notice, that you are trying to use WinForms control in WPF project. And icon class from WinForms doesn't support pack URIs.
Since you want to store your image in your assembly, you can't get it as WinForms Icon easily. WPF usually works with ImageSource class and you can convert it to WF icon (How can I convert BitmapImage to Icon?), but I would call it pain.
NotifyIcon is not implemented in WPF, so I suggest to try this solution.
More detais: Can I use NotifyIcon in WPF?
Why don't you add the .ico file in your solution and point the reference to it something like this
Try doing this
ni.Icon = new System.Drawing.Icon(#"pack://application:,,,/Icon/main.ico");

How to move controls from one window to another one in WPF

I have an application that contains a plugin system. The plugins define their menu and toolbar entries of the main application.
I thought on creating on the plugins a UserControl just to hold plugins menu and toolbar entries (and be able to define them using xaml) to put them on the main application. When I try to attach them to the main Window I get an exception saying
Element already has a logical parent. It must be detached from the old
parent before it is attached to a new one
I've tried to remove first the element in this way:
public System.Windows.Controls.MenuItem Menu
{
get
{
((StackPanel)(_controlItems.Content)).Children.Remove(_controlItems.ItemMenu);
return _controlItems.ItemMenu;
}
}
But I get the same exception. Any thoughts?

Categories

Resources