/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="navigationParameter">The parameter value passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested. /// </param> /// <param name="pageState">A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited.</param> protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) { // Allow saved page state to override the initial item to display if (pageState != null && pageState.ContainsKey("DisplayItem")) { navigationParameter = pageState["DisplayItem"]; } // TODO: Create an appropriate data model for your problem domain to replace the sample data _currFeed = (Feed)navigationParameter; this.DefaultViewModel["Feed"] = _currFeed; this.DefaultViewModel["Items"] = _currFeed.Items; }
/// <summary> /// 读取数据文件中的文章数据 /// </summary> /// <param name="loadFeed"></param> /// <returns>读取到的文章数量</returns> public int loadFeedItems(Feed loadFeed) { loadFeed.Items.Clear(); IXmlNode database = dbDoc.SelectSingleNode("database"); IXmlNode feedXml = database.SelectSingleNode("feed[uniqueId='" + loadFeed.UniqueId + "']"); if (feedXml == null) return 0; XmlNodeList feeditemXmlList = feedXml.SelectNodes("feeditem"); foreach (IXmlNode feeditemXml in feeditemXmlList) { FeedItem feedItem = new FeedItem(); feedItem.Title = feeditemXml.SelectSingleNode("title").InnerText; feedItem.PubDate = DateTime.Parse(feeditemXml.SelectSingleNode("pubdate").InnerText); feedItem.Author = feeditemXml.SelectSingleNode("author").InnerText; feedItem.Content = feeditemXml.SelectSingleNode("content").InnerText; feedItem.Link = new Uri(feeditemXml.SelectSingleNode("link").InnerText); feedItem.IsRead = Boolean.Parse(feeditemXml.SelectSingleNode("isread").InnerText); feedItem.Feed = loadFeed; loadFeed.Items.Add(feedItem); } return feeditemXmlList.Count; }
/// <summary> /// 执行Feed保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void SaveFeedOkBtn_Click(object sender, RoutedEventArgs e) { string title = saveFeedTitle.Text; string uri = saveFeedUri.Text; if (title == null || title.Trim(null).Length == 0) { title = ""; } if (uri == null || uri.Trim(null).Length == 0) { return; } Feed feed = new Feed(); if (editingFeed != null) {// 为编辑Feed操作 feed = editingFeed; } feed.Title = title; feed.Source = new Uri(uri); feed.Background = Backgrounds.Instance.Color; await FeedDataSource.getInstance().saveFeed(feed); // 关闭编辑界面 closePopup(); }
public FeedItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content, Feed feed) : base(uniqueId, title, subtitle, imagePath, description) { this._content = content; this._feed = feed; }
/// <summary> /// 保存FeedItem文章数据到数据文件中 /// </summary> /// <param name="toFeed">文章所属源</param> /// <param name="feedItemList">文章集</param> /// <returns></returns> public void saveFeedItems(Feed toFeed, List<FeedItem> feedItemList) { IXmlNode database = dbDoc.SelectSingleNode("database"); IXmlNode feedXml = database.SelectSingleNode("feed[uniqueId='" + toFeed.UniqueId + "']"); if (feedXml == null) { feedXml = dbDoc.CreateElement("feed"); IXmlNode id = dbDoc.CreateElement("uniqueId"); id.InnerText = toFeed.UniqueId; feedXml.AppendChild(id); database.AppendChild(feedXml); } // TODO 检查是否超过100条,超过就删除,可由用户设置 IXmlNode oldFirstItem = feedXml.FirstChild; foreach (FeedItem item in feedItemList) { IXmlNode itemXml = feedXml.SelectSingleNode("feeditem[link='" + item.Link + "']"); if (itemXml != null) { continue; } itemXml = dbDoc.CreateElement("feeditem"); IXmlNode title = dbDoc.CreateElement("title"); title.InnerText = item.Title; itemXml.AppendChild(title); IXmlNode pubdate = dbDoc.CreateElement("pubdate"); pubdate.InnerText = item.PubDate.ToString(); itemXml.AppendChild(pubdate); IXmlNode author = dbDoc.CreateElement("author"); author.InnerText = item.Author; itemXml.AppendChild(author); IXmlNode content = dbDoc.CreateElement("content"); content.InnerText = item.Content; itemXml.AppendChild(content); IXmlNode link = dbDoc.CreateElement("link"); link.InnerText = item.Link.ToString(); itemXml.AppendChild(link); IXmlNode isread = dbDoc.CreateElement("isread"); isread.InnerText = "false"; itemXml.AppendChild(isread); feedXml.InsertBefore(itemXml, oldFirstItem); } }
/// <summary> /// 将Feed中的所有文章标记为未读 /// </summary> /// <param name="feed"></param> public async Task noReadFeed(Feed feed) { IXmlNode database = dbDoc.SelectSingleNode("database"); IXmlNode feedXml = database.SelectSingleNode("feed[uniqueId='" + feed.UniqueId + "']"); // 找到所有已读的文章 XmlNodeList feeditemList = feedXml.SelectNodes("feeditem[isread='true']"); foreach (IXmlNode itemXml in feeditemList) { noReadFeedItemXml(itemXml); } await saveCache(); }
/// <summary> /// 清除该Feed中保存的所有文章数据,适用于更改URI后 /// </summary> /// <param name="clearFeed"></param> /// <returns></returns> public async Task clearOldFeedItems(Feed clearFeed) { IXmlNode database = dbDoc.SelectSingleNode("database"); IXmlNode feedXml = database.SelectSingleNode("feed[uniqueId='" + clearFeed.UniqueId + "']"); if (feedXml == null) return; database.RemoveChild(feedXml); await saveCache(); }
public void initFeedInfo() { AllFeeds.Clear(); IXmlNode root = feedsDoc.SelectSingleNode("feeds"); XmlNodeList feedList = root.SelectNodes("feed"); if (feedList != null) { IOrderedEnumerable<IXmlNode> feedOrderList = feedList.OrderBy(f => int.Parse(f.SelectSingleNode("order").InnerText)); foreach (IXmlNode feedXml in feedOrderList) { Feed feed = new Feed(); // 编号 feed.UniqueId = feedXml.SelectSingleNode("uniqueId").InnerText; // 标题 feed.Title = feedXml.SelectSingleNode("title").InnerText; // URI string uri = feedXml.SelectSingleNode("uri").InnerText; feed.Source = new Uri(uri); // 颜色 IXmlNode bgcolorXml = feedXml.SelectSingleNode("bgcolor"); if (bgcolorXml == null) { bgcolorXml = feedsDoc.CreateElement("bgcolor"); bgcolorXml.InnerText = Backgrounds.Instance.Color; //feedXml.AppendChild(bgcolorXml); } feed.Background = bgcolorXml.InnerText; feed.Order = int.Parse(feedXml.SelectSingleNode("order").InnerText); this.AllFeeds.Add(feed); } } }
public async Task delFeed(Feed feed) { // 从集合中删除 this.AllFeeds.Remove(feed); // 查找到要删除的源 IXmlNode feedXml = feedsDoc.SelectSingleNode("//feed[uniqueId='" + feed.UniqueId + "']"); IXmlNode feedsXml = feedXml.ParentNode; feedsXml.RemoveChild(feedXml); await saveCache(); }
// 创建用于描述Feed信息的XML节点 private IXmlNode createFeedXml(Feed feed) { // 创建源XML节点 XmlElement feedXml = feedsDoc.CreateElement("feed"); // 创建源唯一编号节点 XmlElement uniqueIdXml = feedsDoc.CreateElement("uniqueId"); uniqueIdXml.InnerText = feed.UniqueId; feedXml.AppendChild(uniqueIdXml); // 创建标题节点 XmlElement titleXml = feedsDoc.CreateElement("title"); titleXml.InnerText = feed.Title; feedXml.AppendChild(titleXml); // 创建地址节点 XmlElement uriXml = feedsDoc.CreateElement("uri"); uriXml.InnerText = feed.Source.ToString(); feedXml.AppendChild(uriXml); // 创建背景色节点 XmlElement bgcolorXml = feedsDoc.CreateElement("bgcolor"); bgcolorXml.InnerText = feed.Background; feedXml.AppendChild(bgcolorXml); // 创建排序节点 IXmlNode feedsXml = feedsDoc.SelectSingleNode("feeds"); IOrderedEnumerable<IXmlNode> nodeList = feedsXml.SelectNodes("feed").OrderByDescending(f => int.Parse(f.SelectSingleNode("order").InnerText)); IXmlNode node = null; if (nodeList != null && nodeList.Count() > 0) { node = nodeList.First(); } int order = 1; if (node != null) { order = int.Parse(node.SelectSingleNode("order").InnerText) + 1; } XmlElement orderXml = feedsDoc.CreateElement("order"); orderXml.InnerText = order.ToString(); feedXml.AppendChild(orderXml); return feedXml; }
public async Task<Feed> saveFeed(Feed feed) { IXmlNode feedsXml = feedsDoc.SelectSingleNode("feeds"); if (feed.UniqueId == null || "".Equals(feed.UniqueId)) {// 新创建的 feed.UniqueId = Guid.NewGuid().ToString(); IXmlNode feedXml = createFeedXml(feed); feedsXml.AppendChild(feedXml); this.AllFeeds.Add(feed); } else {// 对已有的编辑 // 找到旧的Feed IXmlNode oldFeedXml = feedsDoc.SelectSingleNode("//feed[uniqueId='" + feed.UniqueId + "']"); string oldFeedUri = oldFeedXml.SelectSingleNode("uri").InnerText; oldFeedXml.SelectSingleNode("title").InnerText = feed.Title; oldFeedXml.SelectSingleNode("uri").InnerText = feed.Source.ToString(); // 当URI变更后,清除原来的数据 if (!oldFeedUri.Equals(feed.Source.ToString())) { await FeedItemDatabase.getInstance().clearOldFeedItems(feed); } await saveCache(); // 重新加Feed数据 await loadFeedAsync(feed); } return feed; }
public async Task loadFeedAsync(Feed loadFeed) { // using Windows.Web.Syndication; SyndicationClient client = new SyndicationClient(); try { SyndicationFeed synFeed = await client.RetrieveFeedAsync(loadFeed.Source); // This code is executed after RetrieveFeedAsync returns the SyndicationFeed. // Process it and copy the data we want into our FeedData and FeedItem classes. if (loadFeed.Title.Trim().Length == 0) { loadFeed.Title = synFeed.Title.Text; } if (synFeed.Subtitle.Text != null && synFeed.Subtitle.Text.Trim().Length != 0) { loadFeed.Subtitle = synFeed.Subtitle.Text; } if (synFeed.Items.Count != 0) { loadFeed.Description = synFeed.Items[0].Title.Text; } // Use the date of the latest post as the last updated date. loadFeed.PubDate = synFeed.Items[0].PublishedDate.DateTime; // 加载Feed中的文章,并保存到DB中 List<FeedItem> feedItemList = new List<FeedItem>(); foreach (SyndicationItem item in synFeed.Items) { FeedItem feedItem = new FeedItem(); feedItem.Title = item.Title.Text; feedItem.PubDate = item.PublishedDate.DateTime; feedItem.Author = item.Authors[0].Name.ToString(); // Handle the differences between RSS and Atom feeds if (synFeed.SourceFormat == SyndicationFormat.Atom10) { feedItem.Content = item.Content.Text; feedItem.Link = new Uri("http://windowsteamblog.com" + item.Id); } else if (synFeed.SourceFormat == SyndicationFormat.Rss20) { feedItem.Content = item.Summary.Text; feedItem.Link = item.Links[0].Uri; } feedItem.Feed = loadFeed; feedItemList.Add(feedItem); } // 保存到DB FeedItemDatabase.getInstance().saveFeedItems(loadFeed, feedItemList); // 读取DB FeedItemDatabase.getInstance().loadFeedItems(loadFeed); await FeedItemDatabase.getInstance().saveCache(); } catch (Exception) { return; } }