This article is going to explain how to use button control in windows 7 phone application using c# code. Let’s see a brief demonstration on it.
1. Open visual studio and go to File -> New -> Project.
2. Select Silverlight for Windows Phone option
3. Enter the project name and choose the saving location
4. Click on button ‘Ok’
Now click on Toolbox option which is at left corner; drag and drop button control into ‘MainPage.xaml’ user interface.
Now change the title of page according to your convenience and write the button click event code in ‘MainPage.xaml.cs’
Code of ‘MainPage.xaml’:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Home" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Click Me!" Height="72" HorizontalAlignment="Left" Margin="142,118,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
</Grid>
</Grid>
Code ‘MainPage.xaml.cs’:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace testPhoneApp
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(objectsender, RoutedEventArgse)
{
MessageBox.Show("Hello Click Me", "Clicked Event",MessageBoxButton.OK);
}
}
}
Press ctrl+F5 to run your application and after that following output will be appearing.
Now click on button ‘Click Me!’
This is the complete description on Button control in Windows 7 Phone Development. I hope you will be enjoying this article.
Leave Comment