locked
Find a better way to set & get the resource?

    Question

  • I have an XML file which contains some books' information, such as Name, Price, ID, Remark, and this file is stored in the appx package, that means I just read data from it. And these information are frequently used in my App, so I want to set them as a Property of a Class, just like the following code:

    public List<Book> AllBooks {get; set;}

    instead of reading them from StorageFile in a asynchronous way.

    While, here is the problem, the definition of the property does not support the keyword async, so is there another way to place these data and get them?

    • Edited by imnbwd Friday, October 17, 2014 8:48 AM
    Friday, October 17, 2014 8:14 AM

Answers

  • Can't you just load the XML and populate your AllBooks property once when the application starts up? Put your code that reads the XML file in your App.xaml, create a Book object for each book in the XML and add it to the list.

    You could define a static property in an application wide class to be able to access it from any page:

      public class ApplicationService
      {
        public static List<Book> AllBooks {
          get;
          set;
        }
      }

    //read xml...
    Book book = ...;
    ApplicationService.AllBooks.Add(book); 

    The other option is to define an async method that returns the books.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by imnbwd Monday, October 20, 2014 2:29 AM
    Friday, October 17, 2014 12:49 PM

All replies

  • Can't you just load the XML and populate your AllBooks property once when the application starts up? Put your code that reads the XML file in your App.xaml, create a Book object for each book in the XML and add it to the list.

    You could define a static property in an application wide class to be able to access it from any page:

      public class ApplicationService
      {
        public static List<Book> AllBooks {
          get;
          set;
        }
      }

    //read xml...
    Book book = ...;
    ApplicationService.AllBooks.Add(book); 

    The other option is to define an async method that returns the books.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by imnbwd Monday, October 20, 2014 2:29 AM
    Friday, October 17, 2014 12:49 PM
  • This is really a good idea. Thanks.
    Monday, October 20, 2014 2:29 AM