namespace not found in xaml, but can be used in C# code - c#

I have a WPF Project Home,it will reference a library project which defines namespace 'LibraryProjectExample', I want to use it in the Home Project,the namespace can not be found:
xmlns:view="clr-namespace:LibraryProjectExample"
But when I use using namespace LibraryProjectExample in C# code,I can use it normally.
I have checked that the LibraryProject has been referenced by the Home Project, I don't know why i can't use the namespace in xaml.Anyone can tell me,thanks!

You have to use as below in you Xaml page.
xmlns:object="clr-namespace:**namespace**;assembly=*assembly*"
here namespace is your packagename.classname and assembly is your packagename
For Example, xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
Thank You.

It looks like you're not adding the assembly to your xaml reference. Look in your LibraryProjectExample project and find it's package name, then you can append it to your namespace declarationlike so
xmlns:view="clr-namespace:LibraryProjectExample;assembly=YOUR_PACKAGE_NAME"

Besides the namespace you should also specify the name of the library project/assembly where the namespace is defined:
xmlns:view="clr-namespace:LibraryProjectExample;assembly=LibraryProject"
You need to change "LibraryProject" in the above sample markup to the actual name of the referenced project where the "LibraryProjectExample" namespace is defined.

Related

Can't find linked class

I have two projects in the same solution and I have linked a class DatabaseFunctions.cs to the second one.
The problem is that when I reference it in the GUI_Teacher.cs I get the error:
Error CS0246 The type or namespace name 'DatabaseFunctions' could not be found (are you missing a using directive or an assembly reference?) Teachers application C:\Users\user1\source\repos\IASLSN\Teachers application\GUI_Teacher.cs
From the error, you are simply missing a reference.
Take the following steps to add a reference
Right click on the GUI_Teacher.cs project and click on Add Reference
From the left side of the Add Reference dialog, click on Solution
Select DatabaseFunctions.cs from the list and click OK to close the dialog.
In the source file DatabaseFunctions.cs you have something like
using System;
namespace SomeNamespaceName
{
public class DatabaseFunctions
{
}
}
To use the class in your GUI_Teacher.cs source file you need to add either using SomeNamespaceName; at the top or use the fully qualified type name SomeNamespaceName.DatabaseFunctions instead.
Keep in mind that the name of a sourcecode file like your DatabaseFunctions.cs for example has no connection to the name of a class or a namespace in that file. You can put a class like public class DatabaseFunctions { } in a file called foo.cs. And in the using directive at the top of your sourcefiles you provide a namespace and not a filename.

Define xmlns in Xamarin Forms

I have created a Xamarin Forms application. I created another PCL library for keeping UI constants like Color codes.
Portable project name is App.
PCL library project is Utilities.
Defined this in my PCL lib
namespace App.Utilities
{
public class Colors
{
public static Color ColorCode1 = Color.Aqua;
}
}
Tried to define xmlns in xcml file like this.
xmlns:colors="clr-namespace:App.Utilities.Colors;assembly=App.Utilities"
But it is throwing xaml parse exception saying the above namespace cannot be found.
Any help?
XMLNS declaration syntax is correct. Namespace need not include the class name. So In this case Namespace has to be just App.Utilities and not App.Utilities.Colors. Changing it to
xmlns:colors="clr-namespace:App.Utilities;assembly=App.Utilities"
will work provided your assembly name is correct.
You can verify if your assembly name is correct by Right-Clicking on PCL Forms prject > Options > Output (Under Build). There we can see the correct assembly name.

The type or namespace name 'Resource' could not be found

I have a resource.resx file where i have listed the variables in key value pair. In "cs" part of my project the resources is recognized.
But in "razor" part in my "cshtml" file i get error. "The type or namespace name 'Resource' could not be found". What is the correct way of using resource in "razor" code?
#using Resource
....
...
<td>Name </td>
Go to your c# code. Click on Resource class (go to class definition - you need just press F12 when your cursor stays on class name). And check namespace of this class. Put this namespace in #using section.
But I recommend you to put definition of this namespace in pages by default. You can read there how to do it.
I just created new MVC project. Added Resource.resx file with String1 property. Build it. All I had to was calling #Resource.String1 in a view, without using #using directive or adding a namespace.
<h2>#Resource.String1</h2>

Error during Adding Class Library in My Project

I had created one Class library as BLL.Inside that one Class as ClsEmployeeMaster.
I want to use this library in my projet of Visitor Management.
when i was trying to add this as reff. then it added successfully but when I was start to Build then errors occurs as :
Error 5 The type or namespace name 'ClsEmployeeMaster' could not be
found (are you missing a using directive or an assembly reference?)
so What to do??
It is not enough to add a reference to your BLL. If you want to use the classes defined in your BLL you need to name them with the full namespace. Something like
MyCompany.MyBLL.ClsEmployeeMaster em = new MyCompany.MyBLL.ClsEmployeeMaster();
or you can simplify your typing with
using MyCompany.MyBLL;
....
ClsEmployeeMaster em = new ClsEmployeeMaster();
Open the class file that you have created.
Check the namespace written above the class.
for example
namespace MyCompany
{
class BLL
{
...
...
}
}
then add that namespace to your current .cs
using dllName.MyCompany;

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name"?

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name" ?!
As, I'm working on a website, which uses SAP .NET connector. I already added a reference for connector ddl, and while referencing its namespace "using namespace_name", or set class namespace to another one rather than connector namespace,
I got error regarding connector classes with that error message "The type or namespace couldn't be found, are you missing a using directive or an assembly reference?".
But while changing namespace name to connector namespace, everything is going well?!
// Set namespace to be IDestinationConfiguration interface namespace.
// Using this, everything is going well.
namespace SAP.Middleware.Connector
{
public class ConnectorConfiguration : IDestinationConfiguration
{
}
}
// Using that way; it's not working, and got an error regarding IDestinationConfiguration even it belongs to refernced namespace.
using SAP.Middleware.Connector;
public class ConnectorConfiguration : IDestinationConfiguration
{
}
So, connector types forced me to set namespace of class to their namespace!
Is this possible? If so, how?
I ran into this too and couldn't figure it out until I finally found the answer on a post at http://forums.sdn.sap.com/thread.jspa?threadID=1876430, the last line which said:
"Also, make sure your target framework is ".NET v4.0" NOT ".NET v4.0 Client" -- that way you get the System.Web namespace that is required."
My project was targeting 4.0 client target framework. Once I changed it to just .NET Freamework 4.0, all the references worked as expected.
Is this what you are after?
public class ConnectorConfiguration: SAP.Middleware.Connection.IDestinationConfiguration
{
}
You can write all your code without usings if you like, you just need to use the fully qualified namespace name for every class/interface where the using isn't used.
If you try this:
using SAPTEST = SAP.Middleware.Connection;
namespace TestNamespace
{
public class ConnectorConfiguration: SAPTEST.IDestinationConfiguration
{
}
}
If that works, but it doesn't work if you remove SAPTEST, then IDestinationConfiguration must be declared in another namespace too.

Categories

Resources