Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a WPF window from a public static method in a Class Library project (dll)

Tags:

wpf

dll

I want to use WPF windows in future applications instead of Windows forms.

The current setup is as Class Library Project with a public static method. I have added a Windows Form item to the Project. In the public static method I create an instance of the Windows Form Class and use the method ShowDialog.

The reason why I use this setup is that I need my own .net dialog in an other program. This program can only call public static methods i .net.

I have solved the problem by creating a WPF Application Project and added a public Class item to this project. In the public static method i create an instace of the WPF window class and use the method ShowDialog. This works but I would like to create a dll without main-method.

How to show a WPF window from a public static method in a Class Library project (dll)?

like image 441
Stella Andersen Avatar asked Sep 21 '10 09:09

Stella Andersen


People also ask

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

What is class library project in c#?

The Class Library . DLL contains program code, data, and resources that can be can used by other programs and are easily implemented into other Visual Studio projects.

What is the difference between a page and a window in WPF when you are adding a new file in the Solution Explorer?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

When to use class library c#?

Using libraries is useful when you want to share code between multiple programs. If you think you will need your classes to sanitize data in more than one program, this is a good idea to put them in a library.


1 Answers

Class library projects don't have the WPF Window template, so you need to create a UserControl and then change the main tag from <UserControl ...> to <Window ...>. Alternatively, you can create the Window in a WPF application project and then copy the MyWindow.xaml* files to your library project. (In that case, you might need to fix the Build Action property of the Window in the property window. Visual Studio seems to mess that up when copying a XAML file into a non-WPF class library.)

Afterwards, you can show it like you would in an application: In your static method, you create an instance of your Window and call ShowDialog.

like image 197
Heinzi Avatar answered Oct 06 '22 06:10

Heinzi