如何在 Windows Phone 7 应用程序中实现收藏夹?
因此,我有一个从 XML 文件读取数据的应用程序,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<Name>Germany</Name>
<Flag>../Images/Germany.png</Flag>
<ID>1</ID>
<Description>Germany Description</Description>
<Capital>Berlin</Capital>
</Country>
<Country>
<Name>Grece</Name>
<Flag>../Images/Greece.png</Flag>
<ID>2</ID>
<Description>Grece Description</Description>
<Capital>Athens</Capital>
</Country>
...
</Countries>
此数据存储在 List CountryList 中。国家/地区类具有名称、国旗等成员。现在,当在“列表”页面上的列表框中显示所有国家/地区时,我添加了能够单击国家/地区名称并转到“详细信息页面,我们可以在其中查看“列表”页面上不可见的有关该国家/地区的更多信息。
我想要一个星形图标,这样当用户单击它时,应用程序就会将该国家/地区标记为收藏夹。我怎样才能有效地实施这一点?我想到为每个
设置一个
,并进行 TwoWay 绑定,并将收藏夹数据保存到 xml 文件在应用程序作为清理的一部分关闭之前。这是最好的方法吗?有人可以给我举个例子吗?
So I have an application that reads in data from an XML file that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<Name>Germany</Name>
<Flag>../Images/Germany.png</Flag>
<ID>1</ID>
<Description>Germany Description</Description>
<Capital>Berlin</Capital>
</Country>
<Country>
<Name>Grece</Name>
<Flag>../Images/Greece.png</Flag>
<ID>2</ID>
<Description>Grece Description</Description>
<Capital>Athens</Capital>
</Country>
...
</Countries>
This data is stored in List countryList. The country class has members for Name, Flag, etc. Now, when displaying all the countries in a listbox on the 'List' page, I have added the functionality to be able to click on a country name, and be taken to the 'Details' page, where we can view more information about that country that wasn't visible on the 'List' page.
I want to have a star icon, so that when the user clicks on it, the app marks that country as a favorite. How can I implement this efficiently? I thought of having a <Favorite>No</Favorite>
for each <Country>
, and having a TwoWay binding, and saving the Favorite data to the xml file before the app is closed as part of the cleanup. Is this the best way? Can someone please point me to an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法很好。另一种方法是使用一个新的
List
来存储标记为Favourite
的ID
列表。只需检查列表中的 ID 并在它们旁边显示一个星形图标即可。要保存,您可以创建一个简单的文本文件,其中这些值用逗号分隔并保存(或简单地序列化列表)。当需要加载时,可以加载文件(或反序列化)并将其放回列表中。这将节省您对 XML 文件的任何 DOM 操作,并且还意味着您不需要为每个元素存储
No
。您可以尝试这两种方法,看看哪一种更有效,但在空间方面,单独的列表会更好。这还可以缩短 XML 文件的加载时间。
Your method is fine. An alternative could be to have a new
List
that stores a list ofID's
that are markedFavourite
. Simply check which IDs are in the List and show a star icon next to them.To save, you can create a simple text file with these values separated by a comma and save this (or simply serialize the List). When you need to load, you can load the file (or deserialize) and put it back into a List. This will save you any DOM manipulation of the XML file and also means that you don't need to store
<Favorite>No</Favorite>
for every element.You could try both methods and see which one is more efficient, but space-wise, a separate list would be better. This also results in a lower loading time for the XML file.