How To Share Status (Tweet) on Twitter Account in Windows Phone Application Using C#

Introduction

This article shows how to build a Windows Phone application that shares something to a Twitter account using C#.

It is also helpful as a developer if you want to spread your application to the most peole by just providing a control in your application that will share your application market place link on Twitter so that as many people as possible can know about your application.

Procedures

Step 1: Create a new "Windows Phone Application" in Visual Studio and name the project as you choose (I here named it "TwwetPost").

Now a new Windows Phone Application Page (MainPage.xaml) will be generated.

Step 2: Now go to the toolbox and add a Button Control, a TextBox Control and a WebBrowser Control to your project and set the visibility property of the WebBrowser control to collapsed.

Your MainPage.xaml will look like this:

<!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1"

          Margin="12,0,12,0">

        <phone:WebBrowser x:Name="webBrowserTwit"

          Visibility="Collapsed" Margin="0,84,0,0" />

          <Button Content="Twit it!!!" Height="72"

          HorizontalAlignment="Left" Margin="290,6,0,0"

          Name="button1" VerticalAlignment="Top"

          Width="160" Click="button1_Click" />

         <TextBox Height="72" HorizontalAlignment="Left"

          Margin="6,6,0,0" Name="textBox1" Text=""

          VerticalAlignment="Top" Width="297" />

</Grid>

And your MainPage.xaml will look like this:

Windows Phone Application Page

Step 3:
Now it's time to code. Navigate to the MainPage.xaml.cs file of the project and add the following code initially:
 

public partial class MainPage : PhoneApplicationPage

{

    //Declaring a String type variable which will store

    //the Value passed in TextBox

    string twit = "";

    // Constructor

    public MainPage()

    {

        InitializeComponent();

    }

}

Step 4: Now navigate to the Button Event to the project and add the folllowing code:

private void button1_Click(object sender, RoutedEventArgs e)

{

    //Change the Visibility of the WebBrowser to

    // Visible as the Button is Pressed

    webBrowserTwit.Visibility = Visibility.Visible;

     //Passing the text from TextBox to the variable

     // which we have declared

    twit = textBox1.Text;

   //Loading the twitter Page with the status from

   //that TextBox

   webBrowserTwit.Navigate(new Uri("https://twitter.com/intent/tweet?text=" + twit));

}

As a developer if you want to share the link of your apps from the store in a control then you can do it by just removing that TextBox and in the "twit" variable pass the status or your link you want to share from user Twitter account.

That's all for this article. Compile your project and run it . I am including the source file so that you can go through it.

Next Recommended Readings