2012-01-20 40 views
0

我在做一個小應用程序來顯示從XML文件中讀取的一些信息。這些信息很簡單隻包含信息,ID和速率已經和它保存在存儲庫如何在Windows Phone中維護變量

static XmlJokeRepository() 
    { 
     XDocument loadedData = XDocument.Load("FactsXML.xml"); 

     var data = from query in loadedData.Descendants("Joke") 
        select new Jokes 
        { 
         ID = (int)query.Element("ID"), 
         Rate = (int)query.Element("Rate"), 
         Info = (string)query.Element("Info"), 

        }; 
     countryList = data.ToList(); 
    } 

    public IList<Jokes> GetCountryList() 
    { 

     return countryList; 
    } 


    public Jokes GetCountryById(int id) 
    { 

     return countryList.FirstOrDefault(c => c.ID == id); 
    } 

在那裏我有維護財產,並已保存在一個名爲類惡搞類。

public class Jokes 
{ 

    public int Rate 
    { 
     get; 
     set; 
    } 
    public int ID 
    { 
     get; 
     set; 
    } 

    public string Info 
    { 
     get; 
     set; 
    } 
    } 
} 


    interface JokeRepository 
{ 
    IList<Jokes> GetCountryList(); 
    Jokes GetCountryById(int id); 
    } 

其中I呼叫並加載這個XML等上使用該 的InitializeComponent所述的MainPage(); JokeRepository countryRepository = new XmlJokeRepository(); DataContext = countryRepository.GetCountryById(1);

但它的工作,我必須指定哪一個我想要顯示,而不是隻是返回列表並從那裏管理它可以像例如如果用戶點擊下一步,獲得下一個笑話或者其他的東西。我正在尋找一種更通用的方式來做到這一點,並得到我想要的下一個笑話,就像列表並做一個簡單的GetNext()。

此外,在XML中,我只是在文本塊中有綁定笑話。謝謝您的幫助。

+0

我剛剛添加了一個私人的IList 數據併發送了一份清單回來,它完成了工作,謝謝反正。 – Gustavo

回答

0

將數據加載到您的App啓動代碼(在App.cs中)一次,然後訪問當前的笑話或調用一個方法將CurrentJoke設置爲NextOne()會不會更好?

然後,您可以使用OnNavigatedTo()事件來循環笑話。

讓我知道你是否想要一個我在這裏描述的代碼的例子。