Created
December 26, 2010 10:47
-
-
Save kekonn/755342 to your computer and use it in GitHub Desktop.
tutorial pt. 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace InfiniteScroll | |
{ | |
public interface IInfiniteScrollingItemsSource | |
{ | |
void GetMoreItems(int count = 10); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl x:Class="InfiniteScroll.MainPage" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:clr="clr-namespace:InfiniteScroll" | |
mc:Ignorable="d" | |
d:DesignHeight="300" d:DesignWidth="440"> | |
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded"> | |
<clr:InfiniteScrollingListBox x:Name="ScrollingListBox" Width="400" Height="200" > | |
</clr:InfiniteScrollingListBox> | |
</Grid> | |
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
using System.Windows.Controls; | |
namespace InfiniteScroll | |
{ | |
public partial class MainPage : UserControl | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
} | |
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) | |
{ | |
ScrollingListBox.ItemsSource = new ScrollingObservableItemsSource {Username = "notch"}; | |
ScrollingListBox.LoadItems(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Collections.ObjectModel; | |
using System.Xml.Linq; | |
namespace InfiniteScroll | |
{ | |
public class ScrollingObservableItemsSource : ObservableCollection<TumblrPost>, IInfiniteScrollingItemsSource | |
{ | |
private const string BASE_URL = "http://{0}.tumblr.com/api/read"; | |
public string Username { get; set; } | |
private WebClient _webClient; | |
private bool _fetching = false; | |
public ScrollingObservableItemsSource() | |
{ | |
_webClient = new WebClient(); | |
_webClient.DownloadStringCompleted += _webClient_DownloadStringCompleted; | |
} | |
void _webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) | |
{ | |
_fetching = false; | |
// run xml through factory | |
if (e.Error != null) | |
throw e.Error; | |
XDocument doc = XDocument.Parse(e.Result); | |
var posts = from XElement elm in doc.Root.Element("posts").Elements() | |
where elm.Attribute("type").Value == "regular" | |
select new TumblrPost(elm.Element("regular-title")); | |
foreach (var tumblrPost in posts) | |
{ | |
if (!this.Any(x => x.Equals(tumblrPost))) | |
{ | |
Add(tumblrPost); | |
} | |
} | |
} | |
public void GetMoreItems(int count) | |
{ | |
if (_fetching) return; | |
_webClient.DownloadStringAsync(MakeUrl(count,Count)); | |
_fetching = true; | |
} | |
private Uri MakeUrl(int count = 10,int start = 0) | |
{ | |
UriBuilder builder = new UriBuilder(String.Format(BASE_URL,Username)); | |
builder.Query = "num=" + count.ToString(); | |
builder.Query += "&start=" + start.ToString(); | |
return builder.Uri; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Xml.Linq; | |
namespace InfiniteScroll | |
{ | |
public class TumblrPost | |
{ | |
public string Title { get; set; } | |
public TumblrPost(XElement title) | |
{ | |
if (title == null) | |
Title = "No Title"; | |
else | |
Title = title.Value; | |
} | |
public override string ToString() | |
{ | |
return Title; | |
} | |
public bool Equals(TumblrPost other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(other.Title, Title); | |
} | |
public override int GetHashCode() | |
{ | |
return (Title != null ? Title.GetHashCode() : 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment