Xamarin Resource.designer.cs not generating reference for newly add file - c#

I am adding a file called citybase.png to my drawable folder when I set the src property of my ImageView to #drawable/citybase the image is not showing up in the design view. Inspecting the Resource.designer.cs class I realize that no reference is being generated for any files I add to the drawable folder(except for the default Icon of course).
public partial class Drawable
{
// aapt resource value: 0x7f020000
public const int Icon = 2130837504;
static Drawable()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}
private Drawable()
{
}
}
I tried rebuilding and cleaning the project but that does not work. How can I fix this problem? (Xamarin Studio 4.2.3)

The drawable folder name should be in lowercase (in visual studio it always create with name 'Drawable' when creating project), also avoid using such symbols like '#', '#' and so on.

Related

add image in another folder instead of drawable in xamarin cross-platforms

I am able to display image from Drawable folder by using
<image source="live.png"/>
But i don't know how to get image from other folder i create in Resource
Can somebody help me?
Android is very picky about where you can put images, so your best bet is to store your images in the common project. In this example I assume a standard Xamarin.Forms solution with the following projects: Foo, Foo.Android, Foo.IOS and Foo.UWP. Yours will obviously have different names, so you'll have to substitute the Foos...
All the following code will go into the common code project, Foo.
First, create a new folder called Extensions (just to keep your code tidy) and add the following class to it:
[ContentProperty(nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
var imageSource = ImageSource.FromResource(Source);
return imageSource;
}
}
Now add the namespace for this class to your markup:
xmlns:extensions="clr-namespace:Foo.Extensions"
Next, create a folder for your images, again in your common project, NOT in your Android project. You can create subfolders as well. Add your images and make sure that the build action for each image is set to Embedded Resource.
Now you can reference those images in your XAML like this:
<Image Source="{extensions:ImageResource Foo.Images.Subfolder.Bar.png}">
Note that you need to supply the full path of the image, including the project name (Foo in this case) and that folders are separated by dots, not slashes.

Class cannot find another class in the same namespace

I have a C# WebApp that we are doing for a client. I have two classes in the project, defined (in separate files) as such...
A general utility library:
namespace Client.WebApp {
public static class General {
public static MyDbClass GetDB() {
//<creates and returns MyDbClass here>
}
}
}
A PageBase class:
namespace Client.WebApp {
public class PageBase : System.Web.UI.Page {
protected MyDbClass dbo { get; set; }
public PageBase() {
dbo = General.GetDB();
}
}
}
When I try to compile, I get the error from the dbo = General.GetDB(); line:
The name 'General' does not exist in the current context Client.WebApp.
I have double-checked that the namespace is spelled right in both files.
I have tried adding the top-level namespace Client.WebApp as a using directive in the pagebase file.
I have tried fully qualifying the name as in dbo = Client.WebApp.General.GetDB();
Nothing has helped and it insists that my General class does not exist.
Funny thing is that while the build hits this error and stops, the code-editor does not see this error, and the statement does not get red-under-squiggled.
The project is targeting Framework v.4.5.2, and I am working in VS 2015.
It is a strange error, in my VS2015 if I set a file Build Action to anything other than "Compile", I get an error underline on any type for that file.
Anyway the solution here is to verify that the Build Action is set to "Compile", I'm not sure why adding a new file would have set the build action to anything other than "Compile". I've also tested trying to add new files in multiple ways (select a text file template and just name it something.cs), it still sets it as "Compile". You should verify that your VS2015 instance is updated with the latest updates.
I had the same problem: "type or namespace could not be found". It turned out that class was in a file with the "Build Action" property set to "None", meaning the file was not being compiled. I set it to "C# Compiler", that solved it.
This happens to me sometimes. Closing visual studio and opening it again always fixes it for me.
I think I might have figure it out that if we create a file in a folder which suppose to have content files it set that file's build type content automatically. In my Case I create a file under app_code folder and it is a class file but the build type is set to Content which make it unavailable for any other classes i have.
May be it has any other reasons too.
Try to create a new Class in the Project folder and then move it to the folder, you want it to be.

Resources Bitmap file - does not contain a definition for Properties

I have the following code skeleton for a Grasshopper component I am making. Grasshopper 3D is a plugin for Rhino 3D, a piece of architecture software. It's a graphical programming language. Anyways, below is a sample Abstract Class, in which I am adding a Bitmap icon to the component.
namespace HM_SettingsForm
{
public class HM_Settings : GH_Component
{
// Misc code
protected override Bitmap Icon
{
get
{
return HM_SettingsForm.Properties.Resources.heatmap;
}
}
// Misc code
}
}
With that said, I am getting the following error.
Here is my Resources folder:
In my case, I was getting the same Error. What I was doing was I had been adding the image to Resources folder. It was adding it, right. but not the definition. So then I double clicked to the Resources.resx in Properties window. (not the resources folder) then I dragged and droped the image into the Resources.resx window. So That image is copied to resources folder and its definition as well .
Hope it helps
Wow I am silly. I overlooked that I used HM_SettingsForm twice.
Simply doing: return Properties.Resources.heatmap; worked.
Got the same problem, resolved it by referencing the Image (in my case a ToolStripMenuImage) from this:
this.tsm.Image = global::ASIM_Formatieren.Properties.Resources.icon_help;
to this
this.tsm.Image = (System.Drawing.Bitmap)Properties.Resources.ResourceManager.GetObject("icon_help");

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!

ToolStripButton: what's wrong with assigning an image programmatically

There is a Form with a ToolStrip. This ToolStrip contains a ToolStripButton. I want to assign an image to this button:
this.btnSaveFile.Image = Bitmap.FromFile("C:\\Work\\Icons\\png\\save.png");
It works only if there is save.png on specified path. Otherwise, I get an FileNotFound Exception.
If I created a Form via Form Designer, Visual Studio would create a code like this:
this.toolStripButton9.Image = ((System.Drawing.Image) (resources.GetObject("toolStripButton9.Image")));
toolStripButton9.Image here is not a real name. Visual Studio takse my file save.png and transform it into toolStripButton9.Image.
But I create a form programmatically, without Designer. And my question is how to assign an image to the ToolStripBotton programmatically?
I tried to add the image to the project, but it didn't help much. I have no idea how to make Visual Studio grab it and embed into my executable so that I wouldn't need this file on specified location.
In MSDN, I only see the solution like that:
this.toolStripButton1.Image = Bitmap.FromFile("c:\\NewItem.bmp");
But it doesnt' work as I told above. I understand there is a simple solution but don't see it. Could you please give me a hint?
In Visual Studio, Open your "Properties" folder in the solution explorer, then open the Resources.resx file and add a existing image file as resource. You can then use it programmatically via the Resource static class:
Image x = Resources.MyResourceImage;
A full example of the code I suggest:
using System.Windows.Forms;
using Testapplication.Properties;
namespace Testapplication {
public class Class1 {
public Class1() {
Form MyForm = new Form();
ToolStrip MyToolStrip = new ToolStrip();
MyForm.Controls.Add(MyToolStrip);
ToolStripButton MyButton = new ToolStripButton();
MyToolStrip.Items.Add(MyButton);
MyButton.Image = Resources.MyResourceImage;
MyForm.Show();
}
}
}
Don't forget to add a using to YourApps' Properties namespace. Your Resources.resx (.cs) file resides in that namespace and is used to provide strong-types object references like images. In your case, replace "MyResourceImage" with "save" (omit the quotes).
ps. A glance at the most important part of my Resources.designer.cs file:
internal static System.Drawing.Bitmap MyResourceImage {
get {
object obj = ResourceManager.GetObject("MyResourceImage", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
So you mean setting image from an embedded resource?
string res = "MyAssembly.Resources.toolStripButton9.Image";
Stream s = this.GetType().Assembly.GetManifestResourceStream( res );
Icon icon = Icon.FromStream( s );
Use Webleeuws answer if it works, way easier than this :P

Categories

Resources