1

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

enter image description here

Mohamad Mahmoud Darwish
  • 3,865
  • 9
  • 51
  • 76

2 Answers2

5
//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.

Carbine
  • 7,849
  • 4
  • 30
  • 54
  • string[] resources=assembly.GetManifestResourceNames(); Return 0 items ?!? – Mohamad Mahmoud Darwish Mar 01 '16 at 12:36
  • 1
    Your file is not added to the assembly you are checking. check if you are checking for the right assembly or check if you have embedded the resource properly – Carbine Mar 01 '16 at 12:37
  • 1
    does mnakabAlshabaViewBook has reference to mnakabAlshabaViewBook.Droid project? or is it the other way round? – Carbine Mar 01 '16 at 12:50
  • no reference ..when i am trying to add reference i get the following Message : adding the project as a reference would case a circular dependency . – Mohamad Mahmoud Darwish Mar 01 '16 at 12:52
  • that means you have referred mnakabAlshabaViewBook already in mnakabAlshabaViewBook.Droid. You can do this assembly.GetManifestResourceNames() from your Droid project and pass it to the BookPage object – Carbine Mar 01 '16 at 12:57
5

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.

Mohamad Mahmoud Darwish
  • 3,865
  • 9
  • 51
  • 76