Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

samplewpf

Nick Hodge edited this page May 28, 2013 · 7 revisions

Simple WPF Client.

boxkite logo

The source project: Simple WPF Client

Questions? You can find me on Twitter, of course! @NickHodgeMSFT

Screen cap as at 29th May 2013

screen capture

Why?

To test the new, higher level TwitterConnection and the underlying mechanisms of BoxKite.Twitter, I have created a Simple WPF Client.

Using MVVM (simple Singletons, simple Command pattern, binding to a simple ViewModel) this sample is pretty raw. There are at least 1000 features that would need to be implemented to make this a good day-to-day Twitter client.

This client WPF app is not pretty. It doesn't do inline replies or auto-expand URLs; it has been created purely to test out the TwitterConnection API.

The MainPage.xaml and MainPage.xaml.cs contain standard MVVM XAML; binding to a ViewModel and using a simple state machine to progress the Authentication/Authorisation process.

Interestingly, the SearchStream for "twitter" tracks at 48 tweets per second. Yes, I know you cannot read tweets that fast. Interesting that the UI keeps up. 1.3% CPU use. And no massive memory leak either.

Code Example 1:

From ViewModels/MainWindowViewModel.cs

   public class MainWindowViewModel : BindableBase
    {
        // Collections containing the Tweets to be displayed
        // These are populated by ".Subscribing" to BoxKite.Twitter's channels of Tweets/DMs
        public ObservableCollection<Tweet> homeTimeLineTweets { get; set; }
        public ObservableCollection<Tweet> mentionsTimeLineTweets { get; set; }
        public ObservableCollection<DirectMessage> dmTimeLineTweets { get; set; }
        public ObservableCollection<Tweet> searchTweets { get; set; }

...
		
		// wire the BoxKite.Twitter channels to Collections of Tweets
        // Note: "ObserveOn.Synchronization"; ensures all the background tasks getting the messages
        //       pop onto the correct thread where the Collections live. All works async under the covers, so that's nice
        public void Connect()
        {
            mainTwitterAccount.TimeLine.ObserveOn(SynchronizationContext.Current).Subscribe(t => homeTimeLineTweets.Add(t));
            mainTwitterAccount.Mentions.ObserveOn(SynchronizationContext.Current).Subscribe(m => mentionsTimeLineTweets.Add(m));
            mainTwitterAccount.DirectMessages.ObserveOn(SynchronizationContext.Current).Subscribe(d => dmTimeLineTweets.Add(d));
            mainTwitterAccount.SearchTimeLine.ObserveOn(SynchronizationContext.Current).Subscribe(s => searchTweets.Add(s));
            mainTwitterAccount.Start();
...
        }

This is really it. There are many more pieces of MVVM ceremony, init'ing the ObservableCollection, and certainly login and authentication logic. But the simple matter of getting tweets into an app after the connection has been created is pretty darn simple.

All of the underlying complexity of paging/cursoring data is abstracted away. Also the getting older Tweets/DirectMessages and having a Stream to grab new ones - again, abstracted away. Your code just subscribes to the appropriate "channel", and gets the Tweets ready for display.

The ListView in the XAML simply bind the ObservableCollection<Tweet> and display the tweets.

Clone this wiki locally