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.
Related
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.
I currently have a library which was created in a silverlight application for its use. But Now we are switching over to WPF. So i don't know how would i convert the library to a wpf library. Would i just have to copy all the file in a new project(wpf class library)
As you can see when i reference this silver light library in my wpf project. It gives me a warning.
As the message says, you can't use a project compiled to target Silverlight as a reference for a project targeting some other .NET framework family. You will need to compile a separate assembly compatible with the .NET framework family you're using (i.e. a desktop version). This will require the creation of a whole new project (I'm not aware of a practical way to have a single project target both Silverlight and desktop .NET).
Note that the new project can use the same source files as the original Silverlight one. After creating the project (which you should create as an "Empty Project"), you can add the source files from the Silverlight project, by using the "Add Existing..."/"As Link" option for adding items to the project. Adding the source code as links will cause the new project to reference the original .cs files in their current location rather than creating a new copy of them for the new project.
Note also that your Silverlight code may or may not be 100% compatible with the WPF API. You may have to introduce conditional compilation (i.e. use #if, and declare appropriate conditional compilation symbols, in the projects' settings "Build" tab) so that you can provide correct code for each platform in each .cs file.
Related topics (there a lot of duplicate questions involving adding existing items as links…though many of these involve multiple solutions, not just adding items to a new project):
Share c# class source code between several projects
How do I keep common code shared between projects in c#?
Adding Existing Files To Different Visual Studio 2010 Project
Is it possible to statically share code between projects in C#?
Updating classes used in multiple projects?
Make reference to C# code from multiple projects
Share .cs file among VS 2010 C# projects
How to include source files of one project in another project?
I have a project contains a classLibrary and webapplication now what I want is to use this project in another project means I want to add this entire project to another project as dll so developer of newer project cant use, update and see my code they just add in their project and simply use them without knowing whats going on in these dll. I am new to this can someone help me on this. I just to reuse code of webform's code behind and classlibrary .cs files code class which are used and called in webform's code behind.
When you add a class library,
it compiles into a dll.
you can explore the file-system's BIN folder of your project,
and see the dll there after a project build.
What you want to do is take that dll,
and add it as a reference to another project.
Note: You may need to include a using [dll namespace] statement if you want to access
the dll objects / functions without explicitly writing it every time.
I created a mini sample,
here are some screen shots that should guide you through the process.
I'm using VS 2010, but it should be similar 2012 / 2013.
Can anybody provide some code block to add an existing C# file to a project.
I have 2 projects in my solution. One project generates C# class files which will be use by second project. I have to incluse these generated files in the second project and build the project. It should be done through programatically. I know that to include these files I have to edit the C#project file (which is an XML) and make an entry that file. But I thought of
using existing code if anybody has it.
Thanks
You can ahve a look at the MsBuild.Engine namespace. It allows you to manipulate a csproj in a consistent way so you can oper the target project and add the reference programmatically.
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.