Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a WPF application into a class library and start the application when needed

Tags:

wpf

I am wondering how to compile a WPF application into an independent shared class library(dll)? Any sample?

like image 747
user496949 Avatar asked Dec 18 '10 23:12

user496949


2 Answers

  1. Solution Explorer -> Right Click on "MySolution" -> Add -> New Project -> WPF User Control Library
    Add your controls there and use from any other application.

  2. Convert your current project to dll: Solution Explorer -> MyWpfApplication -> Right Click -> Properties -> Application Tab -> Output Type = Class Library

  3. Or you can add references from other Applications to your existing project even if it's exe. (Controls are still available)

like image 91
The Smallest Avatar answered Oct 07 '22 04:10

The Smallest


A library is not an application, so we can't convert a WPF application to a dll and expect to be able to run it independently.

One workaround might be to change the projects output type to "Class Library", then create a new WPF application which hooks into the dll.

To make the new application hook into the dll, first add a reference to the dll, then edit App.xaml and change StartupUri to a pack Uri pointing to the dll.

<Application x:Class="WpfApplication13.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="pack://application:,,,/ReferencedAssembly;component/MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
like image 5
Greg Sansom Avatar answered Oct 07 '22 06:10

Greg Sansom