Blog Posts

Updates, Code Snippets, Demos, and a whole lot of random stuff

Integrating Twitter in Windows Phone App using TweetSharp

June 10, 2011 • 5 min read

In this post we’ll discuss how to integrate Twitter in your Windows Phone Silverlight App and make it more social.

I’ll be discussing the Authorization part and then we’ll send a Tweet.
I’ll be using the Tweetsharp assemblies which is available here. It supports Windows Phone 7, and will probably support Mango too, but keep an eye there.
If you don’t know how OAuth for Twitter works, visit apiwiki.twitter.com. You will also require Consumer Key and Consumer Secret from dev.twitter.com for your App, sign in there with your Twitter account, register an desktop client app and you’ll get the Consumer Key and Consumer Secret.
Now following will be the OAuth workflow of App using Twitter:

1. Get OAuth Request token.
2. Get the Authorization URL and redirect user to web browser and allow User to grant access.
3. Get back the 7 digit code via User.
4. Get OAuth Acces token using Request token and the 7 digit code.
5. Authenticate Twitter Service with Accest token.
Done!

Declaration:

Add all the four Tweetsharp dlls as reference in your Project.
We’ll be using TweetSharp so add:
using TweetSharp;
And now declare the Twitter Service with Consumer Key and Secret and Request, Access Token for Authorization:
TwitterService Twitter = new TwitterService(“YourConsumerKey”,“YourConsumerSecret”);
OAuthRequestToken Request = new OAuthRequestToken();
OAuthAccessToken Access = new OAuthAccessToken();
Getting Request token and Authorization URL:
Now to get Request Token and get the Authorization URL, add:
Twitter.GetRequestToken((request, Response) =>
{
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        Request = request; // Store it for later use

        Uri uri = Twitter.GetAuthorizationUri(request);
       
        WebBrowserTask Web = new WebBrowserTask();
        Web.URL = uri.ToString();
        Dispatcher.BeginInvoke(() => Web.Show());
    }
});
To Redirect the user to Grant access to App, I have used WebBrowserTask instead of WebBrowser control which becomes messy, so add:
using Microsoft.Phone.Tasks;
 
Getting Access token and final Authentication:
Now we have the 7 digit code from user and the Request token, we use following code to Authenticate:
Twitter.GetAccessToken(Request, txtCode.Text, (access,Response) =>
{
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        Access = access; // Store it for reuse

        Twitter.AuthenticateWith(access.Token, access.TokenSecret);
    }
});
Now you will have to save the Access token or the Token and TokenSecret String to Islates Storage Settings or Isolated Storage. The next time you will skip to Authentication.
Sending Tweet! :
Use the following code to send a Tweet:
Twitter.SendTweet(txtTweet.Text, (Status, Response) =>
{
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        MessageBox.Show(“Tweet Sent !”);
    }
});
To access other Twitter Features, visit the documentation of TweetSharp here.
Finally check out my Windows Phone Apps using this code for Twitter Integration here.

If you have any queries, leave a comment or contact me at @Nirmitk26.

CommentsAdd New

Round Button in Windows Phone 7

February 22, 2011 • 4 min read

Its very easy to set up round button like the application bar in Windows Phone 7. It’ll be round (obviously), theme aware and will reverse the colors on press.
I searched for some XAML code to do so, but some of them can just round the edges OR were just theme aware. I combined them and voila ! Application bar like buttons !

  • Open Visual Studio and Windows Phone Application Project. Create a regular button and re-size it to 72 x 72
  • Now open the project in Expression Blend. Right click on the button you created and go to Edit template -> Edit Copy. You will have following dialogue. Rename the name key to “RoundMail” because we’ll insert the E-Mail png in it. Click OK.
  • Again right click on the button and select View XAML because we’ll be altering design via code. Find and delete the following code:

<ContentControl x:Name=”ContentContainer” ContentTemplate=”{TemplateBinding ContentTemplate}” Content=”{TemplateBinding Content}” Foreground=”{TemplateBinding Foreground}” HorizontalContentAlignment=”{TemplateBinding HorizontalContentAlignment}” Padding=”{TemplateBinding Padding}” VerticalContentAlignment=”{TemplateBinding VerticalContentAlignment}”/>

  • Find CornerRadius in Border tag and set the any arbitrary value greater than 24 like this
CornerRadius=”30″
  • Now the button will look like this. We have the round button ! but still empty :(
  • Now copy the whole tag and paste below it and set x:Name=”ButtonImage”. Add mail.png from visual studio’s supplied images for application bars. Add the following code in the ButtonImage border:
<Border.OpacityMask><ImageBrush Stretch=”Fill” ImageSource=”mail.png”/></border.opacitymask>

  • and change the background property to
Background=”{StaticResource PhoneForegroundBrush}”
  • so it will be now
<border background=”{TemplateBinding Background}” borderbrush=”{TemplateBinding BorderBrush}” borderthickness=”{TemplateBinding BorderThickness}” cornerradius=”24″ margin=”{StaticResource PhoneTouchTargetOverhang}” x:name=”ButtonBackground”>
</border>
<border background=”{TemplateBinding Background}” borderbrush=”{TemplateBinding
BorderBrush}” borderthickness=”{TemplateBinding BorderThickness}” cornerradius=”24″ margin=”{StaticResource PhoneTouchTargetOverhang}” x:name=”ButtonImage”></border></div>
<div style=”text-align: justify;”>
<border.opacitymask>
<imagebrush imagesource=”mail.png” stretch=”Fill”>
</imagebrush>
</border.opacitymask>
  • So the button is now theme aware. (You can check by running the project). Still it does not respond to touch.
  • Add this code to the visual state pressed in the storyboard.
<objectanimationusingkeyframes storyboard.targetname=”ButtonBackground” storyboard.targetproperty=”BorderThickness”></objectanimationusingkeyframes></div>
<discreteobjectkeyframe keytime=”0″ value=”0″>
</discreteobjectkeyframe>
<objectanimationusingkeyframes storyboard.targetname=”ButtonImage” storyboard.targetproperty=”Background”>
<discreteobjectkeyframe keytime=”0″ value=”{StaticResource PhoneBackgroundBrush}”>
</discreteobjectkeyframe>
</objectanimationusingkeyframes>
  •  Save and run the project !! :D

You can change the image to the one you want ! It Works !!

Finally check out some of my Windows Phone Apps using this “Round Button”:
http://www.nirm.it/blog/p/apps.html

CommentsAdd New

Bing Live Theme

January 20, 2011 • 3 min read

Google is no more the only search engine I use. Bing is more reliable for Microsoft related queries. Though Bing cannot replace Google, but one more thing Bing’s got is the background image of the search page which changes daily and are really nice (and informative on the US site ). What if you can set these awesome images automatically as your desktop background ? Have a look below …
Do you know what Bing is ? You love the background images ? Run Windows 7 ? This post is surely for you. Other might want to check this because this is a cool Theme for Windows 7.

Follow these steps to set up your live Bing Theme.
1. Download the Bing.theme file from here.
2. Open it, you’ll now be in the Personalization Window.
3. You will see a dialog Box asking for some action. Click on “Download Attachments”.
4. And you’re Done ! You may change the slideshow timings and personalize other settings too.
Enjoy the awesome backgrounds ! And share the link if you like this !
Note :
This theme works on RSS feeds which requires active internet connections. Don’t worry ! The theme will download new images every time you connect to the internet. But it can’t help if you are not connected to internet :(.
Do comment, likes, share and follows my Blog.

CommentsAdd New

CPI Calc 3.0 – Newer and Better

December 11, 2010 • 3 min read

Hi there,

Results are up ! Congratulations ;) Now the CPI thing :

You just have to text me your marks, and you’ll get your grades, SPI and CPI as text reply !! Yep, I have created a message Interceptor which automatically intercepts CPI texts (with keyword CPI – you’ll understand that afterward) to my phone and replies you back.

Just as TV show’s voting system, you have to type some prefix keywords and follow some rules. Oh yeah, it supports just CE, IT and EC marks till date. (I’ll be updating the info. for other branches if added :D ).

These are the rules to send the text :


CPI CE [maths marks] [ae] [oopd] [na] [ee] [1st sem SPI] [2nd Sem SPI]
OR

CPI EC
[maths marks] [le] [ct] [na] [ei] [yoga] [1st sem SPI] [2nd Sem SPI]

OR

CPI IT [maths marks] [ae] [oopd] [na] [ddc] [cpi] [1st sem SPI] [2nd Sem SPI]
to my phone number.

Ex.
CPI CE 75 110 110 110 110 8.88 7.77
OR
CPI EC 75 110 110 110 110 75 8.88 7.77
OR
CPI IT 75 110 110 110 110 40 8.88 7.77

I’ll not be blogging my phone number (Obviously !!). You may get via my facebook profile (Not a friend of me ?? add me :P)

Note :
See that you leave at least one space between keywords, marks and SPIs. Don’t include the brackets too !

Way 2 sms and other free texting sites are supported (If you don’t have free text scheme – that’s pathetic !! :P ). See that the sites don’t add AD crap before your message !

The software won’t be replying if there is any error in your text and won’t even reply you to inform this !

And please, please inform as many friends as you can ! I have 500 free texts everyday for my App to reply !

See ya !!

CommentsAdd New

Checkers – Code in C++

December 3, 2010 • 2 min read

Hi there,

C++ is the first Object Oriented Programming Language we are taught in Computer Science Engineering. Obviously, we had a semester project to do so that they can evaluate our practical knowledge. I had Tic-Tac-Toe in my mind as the first topic, but when I saw my friends making much better games like Twin-Ball, Othello, I had to rethink my project topic. Chess came in mind but was too hard and project had weightage of  hust 50 marks. Checkers came to the rescue. It was more challenging than Othello while obviously easier than Chess, so here I am with Checkers.
I’m happy to reach my own expectations and was able to make a “full on” game “Checkers” with almost 1625 lines of coding. It features both – Two and One ( !!! ) player gaming. The Graphics are the best I can do as we’re not taught, and i referred blogs, books etc.

Continue Reading…

CommentsAdd New

Older Posts, RSS Feed