using XML files in Windows Phone 7 and XNA 4.0 - windows-phone-7

I'm working on a basic tiling engine for XNA 4.0 on Windows Phone 7. I have a bunch of mapdata xml files with all the tile positions, powerup positions etc.
I was wondering what the best way of using these? I've read that if I want to use them with the Content then I have to alter the layout of the xml files.
Is there any way to load these files on to the device within the project and read the data from them?
Many thanks,
ant.

The way I did it was make the XML file an embedded resource, not content, then I could access them in code:
Assembly app = Assembly.GetExecutingAssembly();
XmlSerializer ser = new XmlSerializer(typeof(xmlType));
string[] resources = app.GetManifestResourceNames();
foreach (string resourceName in resources)
{
xmlObject = (xmlType)ser.Deserialize(new StreamReader(app.GetManifestResourceStream(resourceName)));
}
xmlType is a class that represents my XML Format

Related

How to obtain path to Razor Class Lib static assets in controller/library code?

I am migrating an existing .NET Framework application to .NET 6. Parts of the application were UI components in library projects, which I have updated to be Razor Class Libraries.
I have updated the relevant projects based on these microsoft docs:
consume-a-razor-component-from-an-rcl
razor-pages/ui-class
However, I have found a situation where an image, which exists in the RCL project(s), was being loaded in-memory, and modified using the Graphics/Bitmap APIs. I am trying to discover how to accomplish this in the .NET 6 world. I have such static assets properly loading in Views and such at runtime, but I suppose I am wondering how to leverage that runtime lookup in library code (as opposed to in Razor view code).
For example, given an image path to a content file from an RCL project like:
~/_content/My.RCL.Project/Images/foo.png
I can reference such a file in an image tag, like:
<img src="~/_content/My.RCL.Project/Images/foo.png" />
And this works just fine as long as the path is correct - the image is loaded as expected. This is being done successfully for many images in the site thus far.
But how can I load that file into memory in server-side/library code, e.g.
const string filePath = "~/_content/My.RCL.Project/Images/foo.png";
using(var bitmap = new Bitmap(filePath))
using(var graphics = Graphics.FromImage(bitmap))
{
//... omitted
}
I have tried to find the physical file via the IWebHostEnvironment APIs:
IWebHostEnvironment env; //obtained via DI
env.WebRootFileProvider.GetFileInfo(filePath);
env.ContentRootFileProvider.GetFileInfo(filePath);
However, both of these methods return a non-existent file.
Does such a mechanism/API exist for RCL content? Can this be done by calling the Razor engine directly in some form?

Xamarin can not read the file

I'm trying to read a Json file for my mobile app in Xamarin Studio, but it throws me an error
can not find the file
While I try the same in Console Application every thing is all right.
I'm trying reading the file this way:
string Jason = File.ReadAllText(#"C:\Folder\file.txt")
This information is taken from Xam160 Class from Xamarin University. Within mobile application the platform generates a isolated folder area specifically for your App. These are the preferred locations to store files specific to your App with AppHome representing the Root of your sandboxed area.
Android - AppHome/files
iOS - AppHome/Lirbrary/[subdirectory]
Windows Phone - AppHome\Local
To Access these paths you can work with the Environment.SpecialFolder Enumeration to access these locations with the Environment.GetFolder() Method.
//Android
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
//iOS
string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libFolder = System.IO.Path.Combine(docFolder,"..","Library");
// Windows has different implementation. GetFolder Path with throw exception on windows.
string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Each of these will get you access to the path you need to be able to read and write files locally on which platform you are on.

Load Unmanaged dll reference in ASP.NET MVC Core 1.0 (Aka) ASP.net 5 MVC 6

This question relates to an ASP.NET MVC WEP API, and Naudio
what I want
I was working prototype in WPF appliations when I use this code the wav file is converted to mp3
var retMs = new MemoryStream();
using (var ms = new MemoryStream(File.ReadAllBytes("sound.wav")))
using (var rdr = new WaveFileReader(ms))
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 128))
{
rdr.CopyTo(wtr);
}
return retMs.ToArray();
But when using this code in api project I am getting error like this
Unable to load DLL 'libmp3lame.dll': The specified module could not be
found.
I know libmp3lame is unmanged dll, the WPF project I just copy the dll to bin folder and everything works fine, but how can I achieve this in Web API project, I mean asp.net 5 project
Note
the above code is working expected in wpf project
Also now I am only supported in my API is dotnetframwork means windows only
I removed other platform dependencies
Update:
Created Issue in ASP.Net MVC Repo
I'm a little late to the party but I'll share this for posterity. NAudio can convert a WAVE to an MP3 without the unmanaged DLL to avoid this issue all together. The only snag is it has to write out to a file (which can then be read back in). Assuming you have a MemoryStream where you read all the bytes in (like you did):
using (var wfr = new WaveFileReader(ms))
{
MediaFoundationApi.Startup();
MediaFoundationEncoder.EncodeToMp3(wfr, #"C:\Temp\test.mp3", 48000);
MediaFoundationApi.Shutdown();
}
Sounds like an issue with getting the mapping to the module in the bin library path, check out the following as may help -
How to get bin folder in ASP.NET Core 1.0
MVC4 App "Unable to load DLL 'libmp3lame.32.dll'

Opening a PDF file in Windows Phone

I'm developing an app for Windows Phone 7 and I'm using a Phonegap template for it.
Everything looks perfect, but now I’m stuck trying to open a PDF file in the browser.
I tried the following but that doesn’t work because the url of the PDF exceeds the 2048 character limit (it’s a data url). This code runs after the deviceReady event was fired.
var ref = window.open('http://www.google.com', '_blank', 'location=no');
ref.addEventListener('loadstart', function () { alert(event.url); });
Now, I'm trying to save the PDF file to storage and then I'm trying to have it opened by the browser, but the browser doesn't show anything. I'm editing the InAppBrowser.cs code from cordovalib and I added the following lines before calling browser.Navigate(loc);
private void ShowInAppBrowser(string url)
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
FileStream stream = store.OpenFile("test.pdf", FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
var myvar = Base64Decode("the big data url");
writer.Write(myvar);
writer.Close();
if (store.FileExists("test.pdf")) // Check if file exists
{
Uri loc = new Uri("test.pdf", UriKind.Relative);
...
}
}
This code is returning the following error:
Log:"Error in error callback: InAppBrowser1921408518 = TypeError: Unable to get value of the property 'url': object is null or undefined"
I don’t wanna use ComponentOne.
Any help would be greatly appreciated!
You cannot open pdf files from the isolated storage in the default reader for PDF files. If the file is online e.g. it has a URI for it, you can use WebBrowserTask to open it since that will download and open the file in Adobe Reader.
On Windows Phone 8 you actually can open your own file in default file reader for that extension, but I am not sure how that will help you since you target PhoneGap and Windows Phone 7.
Toni is correct. You could go and try to build your own viewer (which would be the same thing as using C1, but with more time involved). I worked on a port of iTextSharp and PDFSharp for WP7, but neither of which are PDF Viewers. They are good for creating PDFs and parsing them some (but to render them there is more work involved). This has been a personal quest of mine, but honestly the best I have gotten is to be able to extract some images from the PDF (and none of the text)
try this
var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assets = await installedLocation.GetFolderAsync("Assets");
var pdf = await assets.GetFileAsync("metro.pdf");
Windows.System.Launcher.LaunchFileAsync(pdf);
This worked correctly on my Device.

How to read Asset Files using VS & Monodroid

I've been trying to implement this example using C# and Monodroid, but am having difficulties reading and writing an Asset file:
http://docs.xamarin.com/android/advanced_topics/using_android_assets
I am using the emulator, not a device.
First of all, I am having trouble finding the namespace for Assets.Open. What I ultimately found was
const string lfn = MyAssetFile.txt;
System.IO.StreamReader(Android.Content.Res.Resources.System.Assets.Open(lfn);
Is this the correct namespace?
Second of all, my Asset file is marked as AndroidAsset and "Copy Always" in the VS "Properties" pane, but my attempts to read the file always fail (File Not Found) using this statement:
string settings = "";
using (StreamReader sr = new System.IO.StreamReader (Android.Content.Res.Resources.System.Assets.Open(lfn))) settings = sr.ReadToEnd();
Do I have my VS settings wrong so that the asset file not being copied onto the emulator, or is it being copied OK but my code to open/read it is wrong?
You must use:
const string lfn = "MyAssetFile.txt";
string settings = string.Empty;
// context could be ApplicationContext, Activity or
// any other object of type Context
using (var input = context.Assets.Open(lfn))
using (StreamReader sr = new System.IO.StreamReader(input))
{
settings = sr.ReadToEnd();
}
If I remember correctly, as I haven't got an IDE at the moment, Android.Content.Res.Resources.System.Assets references the Android assets not your project assets. You want to use your projects assets so you need to use the AssetManager from your Activities or Contexts.
For example: the Activity class has a property called Assets. This is what you use. OR you use a View's Context.Assets property.
Or just simply add at the top:
using System.IO;
and it will work.
Xamarin team forget sometimes to mention using stuff.

Resources