Using files in my Windows Phone app, resource, content..? - c#

I just started on my second Windows Phone application, and I ran into a problem (ehm..challange). In my app, I have added a folder called "Images", and some sub-folders, like this:
Images -> boys -> graphs
The boys folder contains 1 xml file, and the graphs folder contains hundreds of .gif images. And I'm now wondering how to access these files, how can I build them into the .xap file, what's the recommended way to go?
Should I set the files to "Content" or "Resource", "Copy to output folder"?
And how do I refer to these files, both the xml file in "boys", and the gif images in "graphs"?
A bit confused, and hoping that there is someone who can kick me in the right direction :)
edit, more info
I got it working, loading the xml file with the code below, I haven't tried with the images yet, but is this "the way to do it"?
string xmlBoys = #"images/boys/Names.xml";
Uri uri = new Uri(xmlBoys, UriKind.Relative);
StreamResourceInfo sm = Application.GetResourceStream(uri);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sm.Stream);
XDocument data = XDocument.Load(xr);

If you set the BuildAction on the images to Content you can refer to them by the relative path.
e.g.: new Uri("\Images\boys\graphs\img1.gif", UriKind.Relative);
You don't need to "Copy to output folder" unless you need them there for your own purposes. You probably don't. That option is an artefact of other types of project. Everythign you need to distribute will be bundled in the XAP.
Importantly, however, note that WP7 does not have native support for gif format files. I'd suggest converting these to PNG before trying to use them.

Related

Saving images and retrieving them Xamarin iOS

So after recently updating dependencies including Xamarin Forms, I can no longer see saved images within our app in iOS. We use https://github.com/jamesmontemagno/MediaPlugin for adding/taking pictures and have even tried specific OS file writes to environment folder:
iOS specific for saving an image:
public string WriteFile(byte[] data)
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var filePath = Path.Combine(path, CameraHelper.GenerateFileName());
File.WriteAllBytes(filePath, data);
return filePath;
}
The images are saved under an ID that changes after every app update. This obviously breaks our previous method as we saved the image path (which was always the same prior) in the tables and used that to show preview of the images.
Here is the new image path:
"/var/mobile/Containers/Data/Application/215E777E-A624-487E-B687-6647F8C0D1DC/Documents/01_18_2021_04_24_07_98524.jpg"
Old path:
/data/user/0/com.company.ourapp/files/Pictures/
Which essentially makes it impossible for us to see pictures taken from a previous build/version of the app. I have scoured Xamarin Forms/Essentials and Stack Overflow but no luck.
Is there no other way to retrieve the images? Or do we have to set the library paths to be dynamic for our views?
Old path:
/data/user/0/com.company.ourapp/files/Pictures/
It is obviously an Android file path instead of a iOS file Path. Android's file system is different from iOS.
Here is the new image path:
"/var/mobile/Containers/Data/Application/215E777E-A624-487E-B687-6647F8C0D1DC/Documents/01_18_2021_04_24_07_98524.jpg"
This is the right path to iOS document folder in sandbox. See this document about the Application directories in iOS.
I think you should distinguish iOS and Android when retrieving the images.
Which essentially makes it impossible for us to see pictures taken
from a previous build/version of the app.
If the path really changes with different versions, you can get the app version number at runtime and give different filePath to different version.
(While I think your problem is the difference between iOS and Android file path.)

View content of a .resource file

I have a .NET executable and I need to view the resources attached to it. I extracted .resource file from .NET executable using DotNetResourcesExtract utility but I don't now how to view content of .resource file.
Could someone explain how to view this file?
Not sure you're using it correctly...
Assuming your storing images there.
You can simply do:
Image resfile = ProjectName.Properties.Resources.resourceName;
So if resfile is an image, you can put it into an Image control.
so, if you have an image control on your form you can simply do:
imageControl.Image = ProjectName.Properties.Resources.resourceName;
If it's a text file or any other type of file - again, you can access it the same way. If it's a binary file, the ProjectName.Properties.Resources.resourceName will be a byte array, so you 'll need to load it in the correct manner.
Is that what you're wanting? Otherwise indicate what type of file are you trying to extract from your resource file.
According to MSDN The .resx (XML-based resource format) files are
converted into common language runtime binary .resources files
that can be embedded in a runtime binary executable or compiled into
satellite assemblies.
Getting to the point: How to view this?
Well, Since it's a binary file which contains resource(images etc.) therefore you could always use Windows resources editing/extracting applications.
eg. Restorator, Resource Hacker to name a few.
Meanwhile, have look at this Stackoverflow post. which sound almost similar.

Visual C# Express Monogame load image

I am new in monogame.
I loaded an image using Texture2D background;, then go to LoadContent() method and code background = Contect.Load<Texture2D>("background");. I then imported the image in the Content folder.
After that, I compiled it and got this error:
Unable to load background assets
I checked the Image, and it was background.bmp.
I'll keep on looking for a solution for this.
If you have a solution for me please give me a link.
Any help is appreciated. And by the way, I use Visual C# Express 2010.
Basically you have 2 options, you can either add the content to the Content folder directly (if I understand correctly that's what you have done) or you can pre-compile the assets into XNB files first.
If you are using content directly you will need to add the file extension in code like so:
background = Contect.Load<Texture2D>("background.bmp");
And you'll also need to make sure you set the file to Content / Copy if newer in the properties window inside Visual Studio.
As a side note, if you are going to stick with this method I suggest you save your images as PNG files instead of BMP because PNG has lossless compression and supports transparency.
Alternately, you can pre-compile your content files first to store them in a more optimal file format. See https://github.com/mono/MonoGame/wiki/MonoGame-Content-Processing
Right click on the picture, go to Property and you will see. Copy to output. The default is don't copy you need to turn to Copy Always

Filenames in path

I want to load all filenames from a folder in my project to a string array.
But I am not able to. Already tried it with the DirectoryInfo, but I am not able to load the wanted Directory.
Just to be sure. I do not want to copy all files to the IsolatedStorage. I just want to get the filenames.
Could anybody please provide me a solution to do it.
Thanks so far!
WP7 application does not have any API to work with file system.
If you have files in project folder - you can mark them as Resources, and read them usin
Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
If files are marked as Content - you can not get the list of them. You can use only hardcoded names.
If files marked as Embedded Resources you can list them by this code:
var list = Assembly.GetCallingAssembly().GetManifestResourceNames();

C# wpf html help file

C3, WPF, Windows 7, Microsoft Ribbon, Visual studio 2010, dotnet 4.5
I would like to write the help files in html and include them inside the .exe file as an resource.
Is there a way of telling the explorer to get the html pages directly from the .exe file ?
If not, I assume the only way to archive it is to create a temporary directory, copy the
files and start the browser pointing to these files.
Is there a magic call that takes a resource directory and unpack/copy it to a directory ?
Like
tempDir = GetTempDirectory();
WPF_MAGIC_RESOURCE_UNPACKER("/help/*", tempDir );
System.Diagnostics.Process.Start(tempDir + Path.DirectorySeparatorChar + "index.html");
Regards Stefan
I agree with #Tigran, these should be stored separate to the binary or even online, but if you feel as though this is the best solution you could probably make use of the Pack URI functionality of WPF.
This is primarily used for images such as icon resources and splash screens, but could be used for any type of content.
You should be able to extract these resources and save them to the file system or to a stream and use as you see fit.
Example: Uri absoluteUri = new Uri("pack://application:,,,/help/contents.htm", UriKind.Absolute);

Categories

Resources