changing silverlight class library to wpf class library - c#

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?

Related

WPF Application = Exe + DLL + Custom Visualizer ? Can I do all 3 of them?

As the title says, I have a WPF form that I want to build in 3 separate projects:
A Windows application, used standalone by clicking the .exe file
A GUI control that can be imported and used in some other projects (dll)
A Custom Visualizer, that can also be used to view a collection of some specific types (dll)
The interface and all the functionality will be the same(identical) between the 3 projects
My questions are the following:
Can this be achieved by making only one project? Or do I have to make 3 separate projects?
If there is not other way but to make 3 separate projects, how can I have the logic for the controls in a separate project common for all of them?
All the logic is now in the controls events (MouseWheel, button click etc)
I need to use net framework 4.7.2 (or 4.8). Can I make all the projects with this? Or can I also use .net 6.0 on some of them?
Thank you
In such case, you can use a Shared Project. Add a Shared Project (case-sensitive) to your solution and reference that project from other pojects. Then you can access the code inside the Shared Project from other projects as if the code belongs to each of other projects.
Shared Project is often explained in the context of sharing the codes among different platforms but it could be useful for other purpose as well.

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.

Dual project solution, one is dll- embed code into main project?

I writing and open-source winforms app, and my program depends on another open-source project called ObjectListView.
ObjectListView is its own project in my solution and referenced as such, but its output is set as a .DLL.. It's just an enhanced ListView control. Is there any way that the project ObjectListView can be embedded in my own assembly without having to ILMerge or use some form of packer? (Defeating the purpose of an open-source project)
My initial thought was just to manually drop all the source files and whatnot into my own project, but that seems rather clunky and far from an idea solution.
PS; I did try searching for this but I didn't come across much that wasn't related to embedding DLLs into an output assembly.. Not merging projects like this. I apologize in advance if this question has been answered before.
If you don't want a separate DLL, and you don't want an embedded DLL, then perhaps a "Shared Project" project type is what you're looking for. The docs state:
A Shared Project does not get compiled on its own, it exists purely as a grouping of source code files that can be included in other projects. When referenced by another project, the code is effectively compiled as part of that project. Shared Projects cannot reference any other project type (including other Shared Projects).
While I don't think your project is exactly what it's intended for, I think it will work in your scenario.
See What is the difference between a Shared Project and a Class Library in Visual Studio 2015? for more information.

Is there a way to reference a c++ project from a c# project in the same solution?

In my solution we have projects both in c#, that controls some GUI and networking work, and c++, that manages some hardware interactions. In my c# project I have the proper PInvokes and am able to use the c++ output dll with no issue but in order to do it, I have to manually copy the output dll to the build directory or create a build script that manages the copy.
My issue with this method is that the solution, in reality, has many many projects, something like 150 at the moment, covering c++, c, c#, and vb.net. We create and delete projects all the time and managing the copy scripts is becoming a major pain. Especially since not all of the projects rely on each other and we have like 20 different build configurations.
Is it possible to simply have the c# project reference the c++ project and automatically copy the project output the same way it does with other managed projects without using post build scripts?
Well, the way I do it and have always done it is by obviously using Visual Studio, and assuming this C++ projects are VS projects you can easily create a VS Solution containing multiple projects that you can organize with "Solution Folders". The organization of your projects inside the solution is really up to how you want to organize it. It resembles a file system with nested folders. Needless to say that you can host projects in different languages such as C++, C#, VB...I'm not too sure if you can include a C project or not, that's out of my expertise.
See a screenshot below of a solution I created to demonstrate this...
Notice that "Business" has a nested solution folder (Utils) which contains a C++ project (ERM.CPPLibraries) and a VB project (ERM.VBLibraries). Then if you reference projects within the solution (Right click -> Add Reference), you will not need to copy the output assemblies everytime you compile your solution (or project(s)) VS is smart enough to resolve all dependencies, resolve them and update them.
Hope it gives you an idea
Edit based on comment
In simple words...No, it's not possible to reference a unmanaged project from a managed project in a VS solution. You can reference DLLs but not projects itself

How to include source files of one project in another project?

I'm writing a class library in C#/.NET.
I need to compile it for two different frameworks:
4.0 (using for debug myself)
3.5 (using on client).
I want to have one set source files for the two projects, so I can make corrections in 1 copy of files and they are included in the other project automatically.
Now, if I even use "add existing item", VS 2010 creates copies; and I need to copy the latest versions every time.
I can't just change a target in project, because I'm using different versions of .dll references, and because ms vs has some quirks.
You can use the Add as Link feature.
It goes like this:
Right-click where you need your (existing) file to be
"Add" -> "Existing Item"
Select your file then click the arrow on the "Add" button and choose "Add As link" (see screenshot below)
A link to the file will be added to the project instead of a copy
One option is to use the Add As Link options mentioned by the others already, but you have more options than that:
Portable Class Libraries are a special kind of project that allow you to specify which versions of .NET you want to target. The compiler then outputs the respective assemblies for you. The advantage of this technique is that you have one version of the source that compiles to both frameworks. The disadvantage is that you can't use features that the lowest common denominator .NET framework doesn't support.
Source control branching & merging allow you to actually maintain 2 similar but different source files. You have one version that is the master version, then after applying a change to it, you merge it to your projects that produce the actual output. The advantage of this technique is that you can have two completely separate files, so you have a lot of freedom. The disadvantage is that you have two completely separate files, which can be hard to manage.
Do better MsBuild trickery. Using the <choose>/<when> construct you can conditionally include references to a specific assembly version depending on a condition. The target framework version and other fancy settings can also be managed through MsBuild, but you can't always edit these through the UI. You can use this in combination with #if MY_CONSTANT to create conditionally compiled parts of the application
You can create a .NET Assembly that you reference from both projects. You set the .NET version to the lowest version, 3.5 in your case. Visual Studio 2010 and later have multi-targeting support and you can mix and match .NET framework versions in one solution.
What kind of qircks are you running into, if you share (part of) your project files, we might be able to resolve those for you.
When adding file to project choose "Add as link" not just add.

Categories