Change the strings value of Resource FIle in C# UWP - c#

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.

Related

Xamarin.Forms Can not Read Text file from Resources Returned Null

I am using Xamarin.Forms, and I am trying to read text file from Resources, Code Below Returned Null value when trying to fetch text file.
var assembly = typeof(BookPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("AboutResources.txt");
//assembly here return null value, I can not find Text Resources
My Code under the following Class Inherited From ContentPage On PLC Project
public class BookPage : ContentPage
//First get the list of all resources available for debugging purpose
assembly.GetManifestResourceNames()
This will list all the (fully qualified names) of all resources embedded in the assembly your code is written in.
Link: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcenames(v=vs.110).aspx
Check if it has "AboutResources.txt" that you are expecting.
Check if you have incorrectly embedded the resource file, it will not show up in the list returned by the call to GetManifestResourceNames(). Make sure you you match the case of the name.
The following steps working fine for me:
1-First of all I added resource text file to same project
2-The file path must be like "Your Project name.your custom folder.filename".
var fileName = "MySampleProject.XMLData.rawxmldata.xml".
3-Make sure that Build Action is EmbeddedResource.

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

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);

How to embed a file and then save it to a location?

I've added a test.zip to a C# project by creating a Resource1.resx and dragged to the resx tab. It is now visible in the Solution Explorer as a child of Resources.
When the program runs, I'd like to move it from the .exe to a location on the computer like My Documents.
I've a feeling I need to convert the resource to a memory stream before I can write it to file but I'm not sure how to access the file as resource or how to convert it.
I think the following extracts the resource object (then again, it doesn't error no matter what the first param is) but I'm not sure how to proceed:
var resource = new ResourceManager("test", Assembly.GetExecutingAssembly());
Since you activate the resources you have already a ResourceManager.Just use GetObject method,get the bytes of your file and write the them to a new file with File.WriteAllBytes:
var bytes = Properties.Resources.ResourceManager.GetObject("resourceName") as byte[];
File.WriteAllBytes("newFile.zip", bytes);
You should use Assembly.GetManifestResourceStream.
using (Stream x = Assembly.GetExecutingAssembly().GetManifestResourceStream("test"))
{
...
}
Reference to MSDN.

Accessing Resources at Runtime

I have an XML file included as part of my Silverlight 4.0 project that I'd like to access at runtime. I have the file saved in a directory named Resources with the Build Action set to "Content" and the Copy to Output Directory set to "Do not copy". If I decompress the XAP file, I see the XML file in the location I expect it to be, but I'm not sure how to reference it from code. I currently have the following:
Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(#"/AssemblyName;component/Resources/MyFile.xml")
Unfortunately, stream is null after running the code above. In addition to the path mentioned above, I've tried "/Resources/MyFile.xml", "/MyFile.xml" and "MyFile.xml", but they all experience the same behavior.
What is the correct way to access an XML file embedded as a resource in a Silverlight application?
A resource with build action "Content" just gets embedded into the xap file, with the same relative directory structure as the application. It does not get embedded as a resource in the assembly.
When set to build action "Content", you should be able to just load the file using something like (or whatever suits your needs):
XElement.Load(<relative directory>/<file>)
The method you're using currently (using a resource stream) is for embedded resources (which have their build action set to "Resource"). And for those, although I haven't tried yet if your method works, usually you'll get the resources using
Application.GetResourceStream
I have used the code snip below to get access to drawables. Not sure it's completely relevant, but hoping this will give you a hint one way or another ...
Resources res = getResources();
spec = tabHost.newTabSpec("groups").setIndicator("Groups", res.getDrawable(R.drawable.ic_tab_groups)).setContent(intent);
As was mentioned by Willem van Rumpt, "content" resources are not usual resources (they aren't stored in assembly). I've checked out this article and could't found at all that you could reference resource, marked as "content" from other assembly.
So, you have two options:
Define XML as embedded resource
Define XML as resource
In first case stream request looks like:
var a = Assembly.Load("AssemblyName");
var s = a.GetManifestResourceStream(#"DefaultNamespace.Resources.XMLFile2.xml");
In second case:
var a = Assembly.Load("AssemblyName");
var rm = new ResourceManager("AssemblyName.g", a);
using (var set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
var ums = (UnmanagedMemoryStream)set.GetObject(#"Resources/XMLFile1.xml", true);
}
Hope this helps.

Problem with creating ResXResourceSet object from the embedded resource file

I am trying to open a resx file that is a resource in my C# project. I need to create a ResXResourceSet object. However at runtime an "Illegal characters in path" exception is thrown. This is the code I am trying to use.
var resX = new ResXResourceSet(Project.Properties.Resources.ResXFile);
The ResXResourceSet class has only two constructors (from stream and from file name). How can I create an object of the ResXResourceSet class in this situation?
Use Project.Properties.Resources.ResourceManager.GetStream("ResXFile");
If I understand correctly, the value in ResXFile is a string with the complete contents of the ResX, and not a file path, which is what ResXResourceSet expects when you pass it a string. You'll need to wrap a stream around it.
See this question for getting a stream from a string: how to generate a stream from a string?
Also, if you make the resource file into a project item, like the main resources, you can access its ResourceSet through its ResourceManager: ResXFile.ResourceManager.GetResourceSet()
You can add a ResX to your project by rightclicking on the project > Add > New Item > Resources File.

Categories

Resources