Define xmlns in Xamarin Forms - c#

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.

Related

How can I configure default VS Code namespaces for C#?

How can I configure default VS Code namespaces for C#?
E.g. I have the following folders structure:
gameplay/input/controllers/foo.cs
I created the foo.cs by right clicking the controllers folder and selecting New C# Class. And here is how the class looks by default without any changes from my side:
namespace play.input.controllers
{
public class foo
{
}
}
While I would expect the namespace in this case to be gameplay.input.controllers.
I am confused why would VS Code change the gameplay to play? How could I fix it?
UPDATE
I can not find the RootNamespace in my project:
So, the provided answer has no value for me.
You can set the default namespace of a project in Project Preferences->Application. The string that is visible in the "Default Namespace" input box is used for new files in the root directory of the project. If you create items in subfolders, these will be included in the namespace by default.
If you want to remove the application name (play in your case), you can define the default namespace as empty.
You can also edit the project file to include the RootNamespace tag, as follows:
<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp2.1</TargetFrameworks>
<EnableDefaultItems>false</EnableDefaultItems>
....
<RootNamespace>MyRootNamespace</RootNamespace>
....
</PropertyGroup>

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

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.

Invalid Resx file. Could not load type error why?

I'm getting designer error on code:
The Component i'm willing to define a List of properties for:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestProjectForProperty.Test
{
public class MyTreeView : TreeView
{
private List<TypeDescriptorBase> _descriptorsAvailable = new List<TypeDescriptorBase>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<TypeDescriptorBase> DescriptorsAvailable
{
get { return _descriptorsAvailable; }
set { _descriptorsAvailable = value; }
}
}
}
The Descriptor itself:
using System;
namespace TestProjectForProperty.Test
{
[Serializable]
public class TypeDescriptorBase
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
}
I am getting the following error if i try to use the component for example on a form and add any items on the property sheet or in the component's constructor to the DescriptorsAvailable property
Error 1 Invalid Resx file. Could not load type
System.Collections.Generic.List`1[[TestProjectForProperty.Test.TypeDescriptorBase,
TestProjectForProperty, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 which is used in the .RESX file.
Ensure that the necessary references have been added to your project.
Line 134, position 5. ...\visual studio
2010\Projects\TestProjectForProperty\TestProjectForProperty\Form1.resx 134 5 TestProjectForProperty
In the Resx file there is data field with base64 encoded stuff inside when this error is present.
I have been searching for an answer, but the best i got is to restart everything, it didn't help me, do you guys have any suggestions? I'm using .net 4 client and visual studio 2010
In my experience, this is due to a change of version of a referenced library, or a change of the lib itself, which contains the backing type of a property you have defined in your user control. The solution is to "force" the visual studio designer to re-initialize it's designer code for that type, and not expect to retrieve a "canned" version of it from the .resx file of the control.
1) Delete the offending data section in the .resx file of your control. This will be a section in the xml of the .resx file associated with your user control, which has a node: <data></data> - the name attribute will be set to whatever you've named that object in the properties of whatever you added this type to. The <data>/data> section contains a base64 encoded string that is the encoded form of the name and version of the library the type comes from. This is where the problem ism, because it now contains an encoded version of the library and/or version number you are no longer referencing in order to include the type. Delete the entire <data>/data> section, from opening to closing tag, save the change and close the file. Now the "artifact" is gone.
2) Now find the place in the designer file for your control, where the type is instantiated; this is initialization code generated for you by visual studio, and it is the place that is expecting to load a "canned" definition of the type from the base64 encoded string contained within the .resx file. The line will look something like this:
this.myCtrlFoo.MyPropertyFroo = ((MyNamespaceFoo.MyTypeFoo)(resources.GetObject("myCtrlFoo.MyPropertyFroo")));
...now just replace the resources.GetObjec call with the instantiation of a new instance of the appropriate type like so:
this.myCtrlFoo.MyPropertyFroo = ((MyNamespaceFoo.MyTypeFoo)(new MyNamespaceFoo.MyTypeFoo()));
...now save the change to the file, close it, rebuild, and everything should now build & run OK.
Put the MyTreeView and TypeDescriptorBase classes into another project and reference it from your GUI project will resolve the issues.
I'm not sure why exactly the problem occurs - I guess it has something to do with the way the serializing process is generating the base64 string for the DescriptorsAvailable Property. Maybe somebody else can give us some insight.
I've struggled quite a bit with this; I have three user controls that all expose the same non-designer property, but for some reason, any change to two of the three would instantly cause the next build to fail with this same issue. This is in VS 2015.
I wound up having to add the following two attributes to the property that kept expanding in the resx file, and it hasn't occurred since. It works for me because they're not available in the designer anyway.
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
For me, this error occured when I used a custom class as a property for the user control. When I switched from property to traditional get- and set- methods, the error disappeared. I guess this is because properties are already compiled at design-time, so when you build the whole project, a new version of the custom class is compiled which is separate from the one of the control, and the reference is broken.
For me, with the custom class Inventory, all I had to do was to switch from this property-based approach:
public Inventory Resources {get;set;}
to this method-based approach:
private Inventory resources;
public Inventory getResources() { return resources; }
public void setResources(Inventory newResources) { resources = newResources; }
I hope this helps someone, as I've been spending some hours on figuring it out.
In my case I've got the error : "error MSB3103: Invalid Resx file. The specified module could not be found" executed in a light windows container based on mcr.microsoft.com/powershell instead of mcr.microsoft.com/windows:1909 (was working fine on 1909).
The error was on a ressource icon that was compressed with PNG inside.
It can be checked by opening the ressource on visual studio : Project > Properties > Ressources.resx, select icons, double click on the icon, check the end of the title that is either "..,BMP]" or "...,PNG]").
Updating the icon with an uncompressed format solve the "Invalid Resx file" issue.
I stumbled across this question today whilst looking for the solution to a similar issue.
Unfortunately none of the above worked for me, however my issue turned out to be that I had different versions of the .NET Framework for different projects. For example;
Project A - .NET Framework 4.7.2
Project B - .NET Framework 4
Where Project B was referencing Project A. Solution was simply to change the .NET Framework version of Project B to 4.7.2 (in my case) and hey presto the issue was resolved.
A shame Visual Studio doesn't provide a more helpful error message in this case, but something to look out for!

Namespace/Reference errors

My solution wont compile. I am getting an error message when i try to compile my project:
Error 2 The type or namespace name 'Security' does not exist in the namespace 'Base' (are you missing an assembly reference?)
It is confusing, however, because i have referenced the project and that is the correct namespace! here's the solution setup
Solution Base
- Base.Domain
- Base.Security
- Base.Tests
- Base.WebUI
in Base.Security I have a custom role provider file like this:
namespace Base.Security.Providers
{
public class EFRoleProvider : System.Web.Security.RoleProvider
{
//code here
}
}
I have referenced Base.Security in Base.Tests and in Base.Tests I have the following file (that is giving me error):
using Base.Security.Providers;
namespace Base.Tests
{
class Program
{
static void Main(string[] args)
{
var a = new EFRoleProvider();
//more stuffs
}
}
}
I don't get it.. why can I not access Base.Security types from Base.Tests?
Make sure your projects are built against the same .NET version. I've had this issue when adding a reference to a .NET 3.5 project from a .NET 4.0 project.
To check/change the .NET version right click on your project, select properties, and under the "Application" tab make sure the "Target Framework" is identical for each project.
Your code looks correct, it's an issue with the way the reference is set up.

C#.NET Namespace name does not exist in namespace error - only when using is outside local namespace directive - why?

Using .NET 2.0, C#, Windows Forms development, Enterprise Library 3.1.
We have a project namespace (call it Project). We also have several sub-namespaces inside of that project, for example Project.Namespace1, Project.Namespace2, etc.
In one class, we define enums and such to be used with the Enterprise Library Logging block, like this:
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
// enum values here
}
}
}
In another class, I use the enum values so I need to declare a using statement. Same project, so there is no assembly to reference, right?
If I declare the using inside of the local namespace, like this, it works fine:
namespace Project.SomeName
{
using Project.Logging;
// code referencing the Logging enum
}
However, if I put the using statement outside of the local namespace declaration, I get the "type or namespace name 'LogPriority' does not exist in the namespace 'Project.Logging'... Like this:
using Project.Logging;
namespace Project.SomeName
{
// code referencing the Logging.LogPriority.whatever
}
Why is this? Has anyone run across this before?
I have run into similar (though not exactly the same) problems before when using a class that has the same name as its namespace.
Oddly enough it seemed to compile ok on some developers pc's but not on others. In the end we made sure that no namespace contained a class of the same name.
namespace Project.Logging
{
public static class Logging // this is what caused the probems for me
{
}
}
I also had a wired error. I cannot find any namespace which is coming from different assemblies, but begins with executing assembly name.
Finally, I found out that I have set the target framework to .NET framework client profile.
Yes, most likely you have an unusual value set for the "Default Namespace" in your project properties. I would validate the project configuration.
We ran into this issue before and it all went down to ambiguous naming of the namespace and the class name.
When we tried to have our namespace as Services.Web.xxx and also add in a service reference as Services.Web.xxxx and ALSO add a references to an assembly that was named Services.Web.xxx you can only imagine the problems we ran into.
In the end to fix it we simply did a rename to make sure that there was only one instance of the Services prefix
Also you could do the following and create an alias to LogPriority to LogEnum:
using LogEnum= Project.Logging.Logging.LogPriority;
namespace Project.SomeName
{
internal class MyClass
{
public MyClass()
{
LogEnum enum1 = LogEnum.None;
}
}
}
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
None,
Default
}
}
}
It definitely can make a difference if you have usings inside or outside the namespace. There is a good discussion here, and it is likely to be related to your default namespace settings.

Categories

Resources