Xamarin C# - How to reference a resource file programatically from a variable - c#

I'm using Xamarin(C#) and trying to access the files in my Resources/drawable folder so that I can fill an ImageView with that picture. I have all the names of these resources(which are .png files) stored in an array and would like to loop through it to access each one.
I know you can access one file using Resource.Drawable.whateverTheNameIs but I don't want to hard code in the name, I want to be able to get it from a string variable. Is this possible? I tried the GetDrawables method but it's not finding it, and it also says its deprecated so I'd prefer the current way to do it.
Here's the exact code:
ImageView daButton = row.FindViewById<ImageView>(Resource.Id.theButtonThing);
Drawable d = myContext.Resources.GetDrawable(myContext.Resources.GetIdentifier(mItems[position], "button", myContext.PackageName));
daButton.SetImageDrawable(d);
Thanks!

you can find resource Id this way, it works for me:
int resID = Resources.GetIdentifier(mDrawableName, "drawable", PackageName);

Related

Change the strings value of Resource FIle in C# UWP

I have a resource file in my UWP C# Project, I know how to get the value of the string, but I don't know how to change this value, I put this code but it doesn't work.
var resourceloader = ResourceLoader.GetForCurrentView();
var asd = resourceloader.setString();
The resources are read-only in the running app. You will need to change the resource file and rebuild using it.
If you want to save a value persistently you can use the ApplicationData object s.

reading full address using inerop.excel.hyperlinks

I've added some hyperlinks (images) into an excel file (.xls 2003).
I'm using the Microsoft.Office.Interop.Excel Reference to read this hyperlink's address.
But it doesn't show up as the full path location of the image, but only the name (eg image1.jpg).
Here is how I'm doing it.
Below three statements are in a loop.
object index = (object)i;
Microsoft.Office.Interop.Excel.Hyperlink link = links.get_Item(index);
Debug.WriteLine(link.Address);
I think I've found a temporary solution which needs further testing with different locations.
I already have an absolute path to the excel file.
And the hyperlink's address is relative to above excel file.
so here is what I did.
object index = (object)i;
Microsoft.Office.Interop.Excel.Hyperlink link = links.get_Item(index);
string absolutePath = System.IO.Path.GetFullPath(xlpath+link.Address);
Debug.WriteLine(absolutePath);
Bitmap image = (Bitmap) Image.FromFile(absolutePath,true);
This seems to be working for now but not appropriate solution.
I'm still looking for a better solution.

Having Trouble Accessing an Embedded Resource [duplicate]

I have a game application in Visual Studio 2012 C#. I have all the .png images I am using in the Resources file of the project.
Have you any idea why I can access all the files but one by using Properties.Resources?
I checked the full filePath and it's set to the resources folder. And it's added in the program as I did Add -> Existing Item and added the image.
It looks just like the other images. I have no idea why it's not loading. I need this since I need to send a .exe by email to my lecturer and without this image the project is nothing!
I added this in the resource file
internal static System.Drawing.Bitmap grid_fw {
get
{
object obj = ResourceManager.GetObject("grid.fw", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
and although now grid is available, it is returning null :/
Found from: Properties.Resources the icon name does not appear in the intellisense
You also need to add the icon to the Resources.resx file. Open it in
Visual Studio and drag your icon into the Icons menu of the resx and
it will become available.
Also, see Adding and Editing Resources (Visual C#)
You can get a reference to the image the following way:
Image myImage = Resources.yourImage;
If you want to make a copy of the image, you'll need to do the following:
Bitmap bmp = new Bitmap(Resources.yourImage);
Don't forget to dispose of bmp when you're done with it. If you don't know the name of the resource image at compile-time, you can use a resource manager:
ResourceManager rm = Resources.ResourceManager;
Bitmap yourImage = (Bitmap)rm.GetObject("yourImage");
The benefit of the ResourceManager is that you can use it where Resources.myImage would normally be out of scope, or where you want to dynamically access resources. Additionally, this works for sounds, config files, etc.

Portability worries because of custom cursor path in C#

I am using a custom cursor named hand2.cur in my C#-WPF application. I have added the cursor to a folder named Images which has all the images that I use in my application. However I've realized that I cannot add relative path to use my custom cursor as:
Cursor newCur = new Cursor("Images\\hand2.cur");
window.Cursor = newCur;
So I used this:
string absolute = System.IO.Path.GetFullPath("hand2.cur");
Cursor newCur = new Cursor(absolute);
window.Cursor = newCur;
This tries to find the hand2.cur file in the \bin\Release folder. So I added the file there and I got it working.
But the problem is, if I Publish this application and use it on a different computer, it does not work. Now the problem is with the cursor file path, because if I deploy it after commenting those 3 lines, it works correctly. So what do I do to rectify this problem?
I am using other images from the Image folder in my XAML code and they seem to port fine. But then again my knowledge of WPF is limited so if anyone has any ideas, that would help.
EDIT: I have added my Images folder to the project. I have also set the Build Action of the cursor file hand2.cur to Embedded Resource. However when I use the following two lines, I get an XAMLParseException.
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/Slideshow;component/Images/hand2.cur"));
window.Cursor = new System.Windows.Input.Cursor(info.Stream);
The Inner Exception field when I view the details of the error reads: {"Cannot locate resource 'images/hand2.cur'."}
You could make the cursor a resource in your app/assembly and then use GetResourceStream with the pack Uri to the resources location. Pass the Stream of the StreamResourceInfo to the ctor of the Cursor. e.g.
var info = Application.GetResourceStream(new Uri("pack://application:,,,/Images/hand2.cur"));
var cursor = new Cursor(info.Stream);
I've got this working after I added the cursor file hand2.cur to my Resource1.resx resource file. Then I used the following statement in my code:
window.Cursor = new Cursor(new System.IO.MemoryStream(MyNameSpace.Resource1.hand2));

How can I add images into my .resx file?

By other than using AddResource button of Visual Studio, of course.
I submit a form, and an image file with it. In my controller action, I get the HttpPostedFileBase object, and its file name by using Path.GetFileName(). If I just use SaveAs() function to save the image file in my application, I cannot access it later for testing, because it gives me the "Not allowed to load local resource" error.
So I want to add the image to a .resx file and retrieve it later using ResourceManager or something. My question is, how can I add the image I have in the controller action into my .resx file?
I should also ask if this is the good approach for the purpose of adding and retrieving image files in my application.
EDITED:
Ok, using .resx to store images is not a good idea. My original problem was that I could save the image file, but when I tried to show it in my view, it gives "Not allowed to load local resource" error.
Here's the controller code:
public ActionResult UrunEkle(Urun urun, HttpPostedFileBase image)
{
Directory.CreateDirectory(Server.MapPath("~/App_Data/Images"));
var path = Path.Combine(Server.MapPath("~/App_Data/Images"), fileName);
image.SaveAs(path);
urun.ArtUrl = path;
// other stuff....
}
And in my view, I try to retrieve the image like this:
<img alt="#urun.name" src="#Url.Content(urun.ArtUrl)" />
But using Google Chrome, I get this "Not allowed to load local resource". How can I avoid this?
Forget trying to add the image to a RESX file; it's not designed to be modified at runtime.
Please post some code. The most likely thing is that the URL that you're serving up to use the image later is incorrect. Probably because you're returning the path of the file on the server, not a URL to the image within the context of the web site.
The error message from Chrome is telling you that it's trying to load a resource from the local file system on the client.
Url.Content converts a relative virtual path into an absolute virtual path and needs a URL starting with ~. You're currently passing in the absolute physical path to the image file and Url.Content is returning it, unchanged. You need the relative virtual path (~/App_Data/Images/Untitled.png) in urun.ArtUrl. This is the value that you passed in to Server.MapPath.

Categories

Resources