Can I encapsulate all of my dependencies inside my dll project? - c#

My Current Situation:
I have a main project, Project_A,
a dll project Project_Parent_dll
and another dll project Project_Child_dll.
Project_Child_dll contains a wpf user control.
Project_Parent_dll contains a wpf user control, that uses the user control contained in Project_Child_dll.
This Project contains a reference to Project_Child_dll.
Project_A uses the user control from Project_Parent_dll.
Right now, I have to reference both Project_Parent_dll and Project_Child_dll, and that works fine, but when I delete the reference to Project_Child_dll I get an XamlParseException.
Question:
Is there a way I could avoid the reference to Project_Child_dll in my Project_A?

Related

how to add a reference of a user control in a xaml file which is shared by two projects

I have created a class library(CustomMapControl) in the solution and inside this class library I create a UserControl(MapItemsControl).
Now, I added a reference of this class library to both projects (Portogruaro & Trieste).
Here is my solution structure.
Portogruaro is the main project and has all files, Trieste has almost all the files which Portogruaro have added as a reference.
So, the problem is when I tried to use the CustomMapControl and drag and drop it in xaml from toolbox it doenst build and give this error
the name "MapsItemControl" doesnt exist in namespace "clr-namespace:CustomMapControl"
And sometimes it shows this error
Element is already a child of another element
Here is the xaml namespace
xmlns:cc="clr-namespace:CustomMapControl"
and this is the user control in xaml
<cc:MapItemsControl /> <cc:MapItemsControl />
The error changes when I open xaml file from different projects.
The xaml file in which I want to have this UserControl is also shared between the two projects.
I am quite sure that this is a referencing issue.
I have no idea how to reference the CustomMapControl so it will work for both the projects.
You probably have a problem with how your namespace is added in your wpf project. Instead of:
xmlns:cc="clr-namespace:CustomMapControl"
try this:
xmlns:cc="clr-namespace:CustomMapControl;assembly=CustomMapControl"

can't use a user control

I've just added a user control to a windows phone project, but I can't use it.
what I did after creating the control ( the old fashion way: click add --> UserControl, named it RecordList and added a text block and a button in canvas. Simple), I added the namespace in the MainPage of my project like this: xmlns:local="clr-namespace:Project1" and in the Content Panel I added the control like this: . But it gives me an error :The type 'local:RecordList' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
I picked this method from this link
PS: I am a beginner in the software development.
Try adding the assembly in the namespace declaration attribute
"xmlns:local="clr-namespace:Project1;assembly=%Name of the Assembly%" i.e. (Presentation.Infrastructure) where Presentation is the project name, and Infrastructure is the folder where the control lives. –

Create a component for the palette

I have never created a component before, but now have a few which are basically .cs files. They are of type System.Windows.Forms.Control.
But they are only available on the control palette when I am using the solution they are part of. It makes use of a few images which are in the /Resources folder.
Is there a way to make the component into a DLL, so that I can use it in any project by simply referencing it? Or else, make it into a component that always appears in my palette?
You need to create a Control Library project, which is a Class Library (DLL) that contains public classes that inherit Control.
You can then add a reference to the compiled DLL (or to the project if it's in the same solution) and the controls will appear in your toolbox.

Read resources from a DLL file

I've two Visual Basic 2008 projects - one is a class library project and another is a Windows Forms project. In the class library project, I've defined some strings in the project resources (project properties > Resources tab).
I build that class library project and get the DLL file from the debug folder and added up as a reference in my Windows Forms project.
How do I read those strings from the referenced DLL file?
While you can dynamically load the DLL as ho suggests, it's fine to use a reference as you have done. In fact I would recommend using a reference unless you had a particular requirement to dynamically load the resource assembly.
As to accessing the resources there are a few things you need to do.
In the resource assembly you will need to ensure the resources are public. By default resources are set to internal which means you will not see the resources in the winforms app. Double click on Properties\Resources.resx to open the resources view. In the top toolbar you will see a label "Access Modifier" next to a combo box drop down. Change the selection to public.
You will need to reference the assembly from the forms app. You have stated you have already done this. Just a note that a better way to do this is to create a solution that contains both projects. Then in the forms app choose add reference. Click on the Projects tab up the top. Double click on the resource DLL project name. This works better than referencing the debug DLL directly since it means if you change between a release build and debug build in your forms app, it will automatically build a matching release/debug version of your resource assembly.
Once you have added the reference you can simply reference the type out of the resources DLL, e.g.
ResourceDLLNamespace.Properties.Resource.ResourceName
Just a note, you need to be aware of type name clashes if you are using the same namespace for your forms app and resource DLL. In this situation both your forms app will have access to it's own Properties.Resources class as well as that of the resource DLL. You can do two things to avoid this:
Use a different namespace between the two assemblies
In the resource assembly don't include a default Properties\Resources.resx resource dictionary. Delete it and manually add a new resource, i.e. Add New Item and select "Resources File". You should find that you will not be able to add the new resource dictionary to the Properties folder - add it to the root or some other folder as you require. This will automatically give it a different type name by virtue of being in a different folder. You still may want to avoid using the resource file name of "Resources" however, as if you have all the relevant namespaces in scope via using statements you will get the same issue that the compiler won't know which version of Resources to use.
-Donovan
I think you just use System.Reflection.Assembly.Load to load the other assembly then use the constructor of System.Resources.ResourceManager that takes an assembly.
Note, I don't think it needs to a reference for this to work.

Different location of assemblies stopped the type casting

I am writing a custom Control class in C# for my main project.
There're 2 projects, one for my Control and one for my main project. These 2 projects are in the same solution. I add a reference from my main project to my Control project. I notice that the first time after I drag my Control from the Tool Panel onto my main winform, an assembly folder was generated at the C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies, and the folder name is something like "jlebh-py01".
The first build is always OK, but after I rebuild my Control class or whole solution, a new assembly folder will be generated at C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies, and then problem arises, my Control fails to behave well because Visual Studio says that the two types "originates from different location". The error message is as below:
[A]MyControl.TypeXXX cannot be cast to
[B]MyControl.TypeXXX. Type A orginates
from assemblyXXX at location
'C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies\jlebh-py01\MyControl.dll'
Type B originats from assemblyXXX at
location
'C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies\ue4i-z3j01\MyControl.dll'
If I reference the Control DLL directly instead of through project reference, or never rebuild the Control project after use my Control in the main project, things seem to be OK.
Does anyone knows why? Is it the proper way to develop a control and a main project within the same solution?
From what you explained - it seems that the main project in your solution is not updating the reference to the control library that you have. I have been working in ASP.NET for a couple of years at least, and have had similar problems with referenced assemblies, but there was always a very simple fix to it - Rebuild the main project. This should clean it, and then run a fresh build.
Another thing you can try is add a variable assembly version to your control. In the project properties, assembly information, try set the version number to 1.0.* This will force the last two version numbers be based on the day and the time of the day, and each time you rebuild the control- it will have a different version. As long as the main project keeps the version updated - which it should - there shouldn't be any problems..

Categories

Resources