如何在 Windows Phone 中维护变量
我正在编写一个小型应用程序来显示从 XML 文件读取的一些信息。这些信息很简单,只包含它所拥有的信息、ID 和速率,并且保存在存储库中,
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);
}
其中我有一个维护属性的类,并保存在名为 Jokes 的类中。
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);
}
我使用这个在主页上调用并加载这个 XML 等 初始化组件(); JokeRepository CountryRepository = new XmlJokeRepository(); DataContext = CountryRepository.GetCountryById(1);
但为了让它工作,我必须实际指定我想要显示哪个,而不是仅仅返回列表并从那里管理它,以便能够像用户单击“下一步”一样,获得下一个笑话或其他内容。我正在寻找一种更通用的方法来执行此操作并获取我想要的下一个笑话,例如形成列表并执行简单的 GetNext()。
另外,在 XML 中,我的文本块中只有 Binding Joke。感谢您的帮助。
I'm doing a small application to display some information read from an XML file. The information is simple only contains the Info, ID and Rate it has and it's kept in a repository
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);
}
Where I have a class that maintains the properties and are kept in a class named Jokes.
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);
}
where I call and load this XML, etc on the MainPage using this
InitializeComponent();
JokeRepository countryRepository = new XmlJokeRepository();
DataContext = countryRepository.GetCountryById(1);
But for it to work I have to actually specify which one do I want to display, instead of just returning the list and managing it from there to be able like for example if the user clicks Next, to get the Next joke or something. I'm looking for a more generic way to do this and get the next joke that I want like form the list and do a simple GetNext().
Also, in the XML I just have the Binding Joke in the textblock. Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在应用程序启动代码(在 App.cs 中)中加载一次数据,然后访问 Current Joke 或调用将 CurrentJoke 设置为 NextOne() 的方法不是更好吗?
然后,您可以使用 OnNavigedTo() 事件来循环播放笑话。
如果您想要我在这里描述的代码示例,请告诉我。
wouldn't it be better to load the data in your App startup code (in App.cs) once, and then just access the Current Joke or call a method to Set CurrentJoke to the NextOne()?
You could then use the OnNavigatedTo() event to cycle the Joke.
Let me know if you want an example in code of what I am describing here.