Best approach to localize different projects using the same namespace - c#

I'm trying to localize two different projects which share the same namespace.
The Resources files are in project A. Whenever I try to localize a control in project B I get this error:
Error 21 Cannot find the type 'Resources'. Note that type names are case sensitive. C:\data\projects\UIDesktop\MainWindow.xaml
For both I'm adding this reference at the top of each xaml
xmlns:properties="clr-namespace:Gui.Properties"
Is this possible or should they have different namespaces altogether?
thanx in advance

When you only specify the namespace (like "clr-namespace:Gui.Properties"), it refers to this namespace in the current assembly. If you have the same namespace in another assembly, you need to specify the assembly name:
xmlns:properties="clr-namespace:Gui.Properties;assembly=MyAssembly"

Related

How can I move something to "another assembly"?

The situation I am encountering is the same as here:
WPF Custom Namespaces not working
XmlnsDefinition only works for namespaces in other assemblies, not in
the same assembly.
However, I don't really get what is exactly meant by "same assembly" and "different assembly". If AssemblyInfo.cs and the XAML files are in the same assembly, how can I move AssemblyInfo.cs away from the assembly to another?
Documentation does not refer to such any limitation.
Mapping CLR Namespaces to XML Namespaces in an Assembly
WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XAML namespace. This attribute, XmlnsDefinitionAttribute, is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces, such as System.Windows and System.Windows.Controls, to the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.
The XmlnsDefinitionAttribute takes two parameters: the XML/XAML namespace name, and the CLR namespace name. More than one XmlnsDefinitionAttribute can exist to map multiple CLR namespaces to the same XML namespace. Once mapped, members of those namespaces can also be referenced without full qualification if desired by providing the appropriate using statement in the partial-class code-behind page. For more details, see XmlnsDefinitionAttribute.
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml#mapping-clr-namespaces-to-xml-namespaces-in-an-assembly

WPF - src type not found

I tried to use this link: http://msdn.microsoft.com/en-us/library/ff407130.aspx . But whenever I debug it for some reason it throws an error :
The type 'src:MyVirtualizingStackPanel' was not found. Verify that you
are not missing an assembly reference and that all referenced
assemblies have been built.
And another error:
'src' is an undeclared prefix. Line 13, position 22.' XML is not valid
I copied the class and methods into the MainWindow class and the XAML.
Am I missing anything here?
At the end of the day, you are mistaken about your XML Namespace. If you type your error minus the name into a search engine, you will see something:
The type was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
All of the results will point to the fact that you have not provided the correct XML Namespace for your project. Now we don't have your project in front of us... only you do, so only you can work out what your Namespace problem is. You mentioned something in a comment that made me suspicious.
You said The name-space is set automatically, but when I asked you to tell me exactly what that meant, you didn't, so I still don't know what you meant. Either way, it doesn't matter, but that could be a cause of your problem. Here are some other possible causes:
Maybe your MyVirtualizingStackPanel class is defined in a different class/project to where you specified.
Maybe you need to include the assembly=XXX part of the XML Namespace.
Maybe you have not included the CLR Namespace in your MyVirtualizingStackPanel class.
Either way, this is definitely an XML Namespace problem, so just keep looking/experimenting and you'll find the problem eventually. You can find out exactly how to add an XML Namespace from the XAML Namespaces and Namespace Mapping for WPF XAML page on MSDN.
Yes, you need to specify the namespace where the MyVirtualizingStackPanel class comes from.
In the beginning of your XAML, add:
xmlns:src="clr-namespace:XXX"
where XXX is the namespace.
See MSDN.
You have to import in the xaml the namespace srcto use this
On the top of the page you do:
xmlns:src="yourProjectPath"
than you can use the MyVirtualizingStackPane class.
add the XMLNS in the top,
Here look at the example
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Add a reference to 'PresentationFramework' - you will find it in:
Add reference... -> Assemblies -> Framework

why we give reference to the other projects?

When looking at a solution with multiple projects:
1) Why do we add a reference to the other project? Can't we just use inheritance?
2) After we add the reference by using Visual Studio, why do we have to add the project to the namespace system? For example: using myReferenceProject; I thought that the IDE would do that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myReferenceProject;
using System.Data.SqlClient;
using System.Data;
1) why we give reference to the other project? cant we just use inheritance???
They're two completely different concepts.
Adding a reference to another assembly basically says, "I want to be able to use some of this code. Please make the compiler aware that this code exists, and optionally copy it into the output directory so that it's present at execution time too."
How would you expect to use inheritance to create a class derived from some type if the compiler has no knowledge of that type?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system???
Because assemblies and namespaces are different concepts too. A using directive simply says to the compiler "When I refer to type Foo within my code, look in these namespaces to try to resolve it." That type could come from any of the assemblies you've referenced.
It's really important that you understand the difference between assemblies and namespaces. Even though they're often named similarly (Foo.Bar.dll often provides types in the namespace Foo.Bar) they're conceptually different.
The project is a self sufficent compilable unit, that has to compile into the valid assembly file. That's why you need esplicitly specifiy which referencies every project needs in order to be able to compile separately from others.
There is no any inheritance concept on projects level.
1) why we give reference to the other project? cant we just use inheritance?
This question makes no sense. What does inheritance have to do with project references. Can you elaborate?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system?
Because there's an inherent difference between an assembly referencing another assembly (which is what happens when you add a reference to the project) and the language knowing where to find a class which is what happens when you use the using directive in a file.
For example, suppose you create a class in your project called TextBox. Then in another file you want to use that class. How would the language know whether you are referring to your custom TextBox class or another one in a referenced assembly? The answer to that question is namespaces. By fully-qualifying the class name with its namespaces, you tell the compiler which class you're using.
The using directive is a way to specifying the namespace once per file instead of every time you use the class (or other classes in that namespace). So if you need to reference your TextBox class multiple times within a single file, you wouldn't want to have to write this every time:
MyCodebase.MyAssembly.MyNamespace.MyOtherNamespace.SomethingElse.TextBox
Instead, you include a using directive of the entire namespace, so you only have to write this:
TextBox

c# error The type or namespace name 'SampleMain' does not exist in the namespace

Error 7 The type or namespace name
'SampleMain' does not exist in the
namespace
'System.Windows.Forms.DataVisualization.Charting.Utilities'
(are you missing an assembly
reference?)
i am getting this error on this line:
System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm mainForm = (System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm)this.ParentForm;
what am i doing wrong?
If you need help figuring out the 'structure' of the namespaces, use the Object Browser from within Visual studio or use Reflector to view what's within the
System.Windows.Forms.DataVisualization.Charting.Utilities
namespace. This may help you gain an understanding as to what you can and cannot access within each.
I get this error when I have no reference to the library in the "References" folder in my project. Check and make sure you are referencing it by clicking on the + next to the References folder and expanding the list.
Some of the libraries require you to explicitly reference them, even if they are contained in a namespace you are already referencing.
Edit: Also, make sure that the namespace/class you are trying to use really exists.
Edit: 'SampleMain' may not even be a good object to use. Found this that suggests that you shouldn't reference sample environment classes: http://social.msdn.microsoft.com/Forums/en/MSWinWebChart/thread/26aac6f7-d3bf-492f-bb52-dc88477f1b1b
Did you add the dll reference to your project?
click on the + next to Reference to see if you have included the correct namespaces/assemblies
You're getting this error because you've referenced a type with the following name that the compiler cannot find
System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm
The most likely causes for this issue are
It exists but it's in a DLL that you haven't referenced in this project. Check the references of the project and make sure the DLL that contains the type is listed
The type name is simply incorrect. Could be a typo or a just a wrong namespace in the name.
The SampleMain type does not exist in System.Windows.Forms.DataVisualization.Charting.Utilities namespace.
Always check that you do not have one of your projects set to the client profile. That will cause this same sort of behavior.

c# Where can I lookup Microsoft Namespace names?

I'm using a MS namespace but Visual Studio is telling me I don't have a reference to it. Is there a place you can go to and lookup namespaces?
Thanks
If you mean "to find which dll I need (per-type)": MSDN?
For example, CLSID
Namespace: Microsoft.Aspnet.Snapin
Assembly: AspNetMMCExt (in AspNetMMCExt.dll)
You can normally find the MSDN page about a specific namespace by going to http://msdn.microsoft.com/namespace. So, for example, to find out about System.Web you could go to...
http://msdn.microsoft.com/system.web
That in itself doesn't help you. You'll need to click through from there to the specific types you're using, and it'll tell you (near the top) the name of the DLL that implements the type.
Remember that a namespace can contain types that are defined in more than one DLL.
See http://msdn.microsoft.com/en-us/library/wkze6zky(VS.80).aspx for how to add a reference
You will also need the assembly.
For Microsoft and System namespaces the easiest way is http://msdn.microsoft.com/library or, if MSDN installed locally, its index.
If you want to know which assembly a certain class is located in you can simply check the documentation (it's noted on the class overview page of the class). Note that one namespace might very well be spread out over more than one assembly.
You need to first add reference to the DLL before using it in your code with the 'using' keyword.
Right click the project > add reference > in the .Net tab select the component and click ok. Then build your code.
You can't find the DLL for a specified namespace in all cases because multiple types belonging to the same assembly may reside in different assemblies.
The fastest way to get there would be to google to the MSDN page for the specific type (class) you are using. Let's say XDocument.. I put `msdn xdocument class' into google. First result is the page I need. Click!
Under the class name you'd see a section like this
Namespace: System.Xml.Linq
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
This shows you the namespace that the type belongs to (for which you may need to add a using in your code)
and the DLL you need to 'Add Reference' to.

Categories

Resources