C# WPF Library creating - c#

I have a WPF project. I want to use this project in other new projects using a .DLL file, Like MessageBox form in C#, when we use MessageBox.Show to create a form.
Library project file cannot specify ApplicationDefinition element.
When i change the output of the project to "Class Library", the "InitializeComponent();" method makes an error, so does many other methods in the constructor of my window, saying
The name 'InitializeComponent' does not exist in the current context
How do i solve this? and how do i use my library once created?

You can't simply port it to a Class Library. You can hack at the app.xaml and so forth, but it would be best if you create a new "WPF User Control Library" or "WPF Custom Control Library". Migrate over your existing code to it. Make sure to expose Public classes and methods that you wish to call from outside of the library.
You can create a Solution to contain your new library as well as a test WPF Application. That way you can add a reference in the application pointing to your library project. Testing would be easier that way.
As far as how to use the library -- you'll need to do some research on that. There are a number of ways to go about it depending on your needs.

OK, the problem is that you project is an app, not a library. Just changing it to library in settings wont help.
The best this to do is to create a new project of type 'user control library', and then copy all of your xaml and classes over to the new project

Deleting App.xaml from my project worked.

Related

c# winforms - calling form from another dll (c#) missing show() and showdialog()

I have some project (app) in c# winforms(.net framework 3.5). Project is builded from few smaller projects. In all this smaller project there are some forms. Right now I need create seperate project (app) and I want use one of this smaller project by his dll.
I adding dll to project by reference, I see form that I need so I create object. I see some my custom proporties but I don's see basic methods like Show() or ShowDialog(). Honestly I don't know were is problem. I tired some someone else's dll from different source and different framework's and It's worked. So I'know there is problem in my 'main' app but I don't remember any proporties which disable visibiltes of this methods. What can I do to get all the methods back?
Project which I try using dll are library type. When I'm using it in that main app I see all methods.
Maybe I show screen:

Self-built C# Library not found

I have an issue including a self-built library to a C#-project. I have created an own class library called ClassLibrary1 just to learn how to add libraries on Visual Studio 2019.
So I have written some simple code in a newly created .NET-class library project and have clicked on "create new solution" (directly translated from my german IDE-language. Maybe it's called slightly different) after writing the code. Back in the C#-project, I have selected the dll-file from bin/Debug/ of the class library's project folder.
After I have set the checkmark, the dll-file was shown in the solution-explorer under Assemblys like expected. But the issue I now have is that I still cannot use the ClassLibrary1.dll-file in the cs-file in this very project as I expected via the command "using ClassLibrary1;". It only shows me the error message "type- or namespacename "ClassLibrary1" not found" when trying to compile the C#-project and I don't get, why this is the case.
It seems like it has to be a very obvious problem but after some research on the internet and trying some things by myself still nothing has changed.
Thanks in advance for helpful replies.
The by far easiest way to manage a library is to use project references. Ensure that your library and the project that uses the library is in the same solution. Then right click the "references" and select "add Reference", go to the project tab and add a checkbox for the library. Read more about managing references.
You might also need to add namespaces for the classes you wish to use in the source files.
I would not recommend managing using file-references to lose dll-files, since it can easily become a hassle to manage. I.e. if you create a new version of the library you would need to build, and explicitly replace this file in all other projects and update all the references.
If you want to share libraries between multiple solutions the more popular solution would be to setup a nuget server. This solves some of the updating problems by maintaining multiple versions of the same library, and provides a nice interface to update references in all projects. But this is a somewhat more complicated solution, so I would not recommend this for new developers.

Converting part of project into a dll

I have made a C# project as a MVVM (Model-View-ViewModel), and i need to convert the model part of my project into a dll.
I already know that i can go and change the output type of the project to Class Library, but this gives me some problems in my ViewModel as it tries to convert the entire project into a dll.
The model part is only .cs files.
You should add a new class library project to your solution, move the .cs files you want to separate to the new project, and then add a reference to the new project in your original project.
Since you are following MVVM you can add WPF Usercontrol library project.
This is an another way of creating library file which includes UI.

Building wpf application as type of wpf class libarary

My need is to develop an addin for an application
but WPF application is not allowing to build it in type of WPF Class Libarary
Refering to this question i found that without XAML pages only we can build WPF application in Class Library Type. It it true? Or am I missing something?
I need to add a WPF window to my addin, but they are referring to remove all the windows and add usercontrol? Whats the workaround for it? Or am i doing anything wrong?
What i did now is I just deleted my application.xaml window from my solution and changed the target type to WPF class Libarary and builded the solution and it builded successfully. Is it correct way..? or any other ways there..? Am really new to this WPF !
There two additional templates to build WPF dll's such as WpfCustomControlLibrary and WpfControlLibrary. You will then be able to add a Window (and other WPF-specific elements). And it can certainly contain general-purpose classes. And it will be built into an assembly no different from that from general class library project.
How to: Create a WPF UserControl Library Project
How to add a WPF control library template to Visual C# Express 2008
If you want just create class libary, please, just select Class Library
How to: Create Class Library.
Also, you can add Window to general class library project. Visual Studio just does not expose Window from add new items dialog. A workaround is to add a User Control Item, and then change it to derive from Window.
Yes, you can go on creating a Class Library, adding a XAML view there. In your exe project you will include a reference to that lib and its public XAML windows or user controls should be available under the library's namespace, like any C# class.
Edit
A simple example from GitHub: as you can see, the library project is compiled with a reference to the System.Xaml assembly.
Edit 2
To build an already existing Windows Application project into a Class Library one, besides changing the Output type you have to delete only the App.xaml

Reusing code in .NET website (app_code folder)

I need to use some classes inside the app_code folder of a website project in visual studio 2008. I need to access this code from a class library project in the same solution. I cannot add a reference to the website, and I'm not sure of the easiest way to use the classes that already exist here. Do I need to move it to a class library?
What other options do I have?
Yes, create a class library and move any types you need into that library. This library can be referenced in as many places as you would like.
The best way to do this is to put those classes in their own library.
However, if you really don't want to do that, you could add a link to the files in the library project. To do that, right-click the Class Library project or a folder within it, Add, Existing Item, navigate to the code files, click the down arrow near the Add button, Add As Link. This will add the same file to both projects. You can even use the #if preprocessor directive to limit portions of the file to specific projects.
However, it is vastly preferable to put the code in a library and reference it in the web project.

Categories