locked
Help with creating class with preloaded data

    Question

  • Hi,

    I have a class where I want to pre-load some data for the user upon launch.

    I do this by having my class and then upon initialize for each page I create a new list and insert the same data.

    The issue I have is that I have multiple pages but obviously only want to enter data once and then fetch the data from the class.

    Simlar as the datasource-sample used by the cookbook.

    How do I turn my class into some sort of datasample that I can then get the same data no matter which .xaml.cs page I load?

    This is my class where I create different dogs.

    theClass.cs

    namespace dogAPP { class dogs { public string dogID{ get; set; } public string DOG{ get; set; } public exerciseMaker(string dogID, string DOG) { dogID = DOGID; dog = DOG; }

    } }

    And then I simply create a new list and add the dogs.

    Sunday, March 02, 2014 1:42 PM

Answers

  • Hi,

    You can find in SampleDataSource.cs file in StoreData constructor add the data to the collection which is the property of StoreData class. If you want to use these data, no matter in mainPage.xaml.cs or mainPage2.xaml.cs,you only define a StoreData object and get the collection property. See Scenario1.xaml.cs and Scenario2.xaml.cs in  XAML GridView grouping and SemanticZoom sample 

     _storeData = new StoreData(); 
                 
                // sets the list of categories to the groups from the sample data 
                List<GroupInfoList<object>> dataLetter = _storeData.GetGroupsByLetter(); 
    
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, March 04, 2014 8:08 AM
  • To elaborate,

    You can create  a collection and add items to it in a function - say we can call that function GetItems() :

    public class Test {
    
    public List<Items> GetItems()
    
    {
    
    //return the list here
    
    
    } }
    

    In mainpage.xaml.cs , you can create an instance of class Test and call the GetItems() methods as :

    Test obj = new Test();
    var items = obj.GetItems();
    
    

    And if you'd like to pass the items to another page via navigation, you can do so by passing those as parameters through the Frame.Navigate method : 

    this.Frame.Navigate(typeof(Page2),items);

    Sagar

    Tuesday, March 04, 2014 9:04 AM
  • You can also create a static object in App.xaml.cs and reference that across the entire app:

    public static List<dog> DogList = new List<dog>();

    In mainpage.XAML.cs, you could reference it like this:

    App.DogList.add(new dog());

    dog CurrentDog = App.DogList[0];


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.



    Thursday, March 06, 2014 3:12 PM
    Moderator

All replies

  • Hi

    According to your description, you want to fetch the data in any page which the data already loaded or defined elsewhere. You can create a class, and defined or loaded the data in the class constructor.
    If you want to get the data in a page, you only need to define an object which its type is the class in the page. You can see XAML GridView grouping and SemanticZoom sample , in SampleDataSource.cs file there have a StoreData class which can define or load data.

    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, March 03, 2014 7:12 AM
  • Hi Anne! Thanks for the info. Yen its true about the sampledatasource however I dont understand it.

    If I create a new list with some dogs in my class, how do I get that list to my .xaml.cs pages? I never managed to move one list from a ie mainPage.xaml.cs to mainPage2.xaml.cs

    Tuesday, March 04, 2014 5:23 AM
  • Hi,

    You can find in SampleDataSource.cs file in StoreData constructor add the data to the collection which is the property of StoreData class. If you want to use these data, no matter in mainPage.xaml.cs or mainPage2.xaml.cs,you only define a StoreData object and get the collection property. See Scenario1.xaml.cs and Scenario2.xaml.cs in  XAML GridView grouping and SemanticZoom sample 

     _storeData = new StoreData(); 
                 
                // sets the list of categories to the groups from the sample data 
                List<GroupInfoList<object>> dataLetter = _storeData.GetGroupsByLetter(); 
    
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, March 04, 2014 8:08 AM
  • To elaborate,

    You can create  a collection and add items to it in a function - say we can call that function GetItems() :

    public class Test {
    
    public List<Items> GetItems()
    
    {
    
    //return the list here
    
    
    } }
    

    In mainpage.xaml.cs , you can create an instance of class Test and call the GetItems() methods as :

    Test obj = new Test();
    var items = obj.GetItems();
    
    

    And if you'd like to pass the items to another page via navigation, you can do so by passing those as parameters through the Frame.Navigate method : 

    this.Frame.Navigate(typeof(Page2),items);

    Sagar

    Tuesday, March 04, 2014 9:04 AM
  • You can also create a static object in App.xaml.cs and reference that across the entire app:

    public static List<dog> DogList = new List<dog>();

    In mainpage.XAML.cs, you could reference it like this:

    App.DogList.add(new dog());

    dog CurrentDog = App.DogList[0];


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.



    Thursday, March 06, 2014 3:12 PM
    Moderator
  • You guys are Quick to put in an answer as correct!

    I've tried back and forth and Went with this solution.

    Friday, March 07, 2014 4:48 PM