|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is part 1 of the tutorial, but there is also Part 0. You can access it through the following link: Part 0. Part 0 of the tutorial was at an introductory level. Here, we will switch to the more advanced concepts. In particular, we will assume that the readers are familiar with C# and most of the OO concepts it uses, e.g., inheritance. We will also assume that the readers have some basic knowledge of Visual Studio 2008. This installment will include multiple simple examples showing how to use Silverlight elements: panels and controls, while, at the same time, displaying the ways Silverlight works. Overview of Panels and ControlsSilverlight 2.0 comes with many built-in objects ready to be used for building a business logic application. Such objects are sub-divided into Panels and Controls. Panels are GUI objects that contain other GUI objects (their children objects). Practically, any visual object can be placed within a panel: other panels, controls, etc. A panel's main purpose is to position its child objects. Controls usually serve to trigger some business logic behavior of an application or to provide (and change) some business logic values. For a comprehensive demo with Silverlight 2.0 Beta 2 controls, check this site. Both Panels and Controls derive from the
ControlsIn this section, we are going to consider individual Control examples. ButtonThe Here is the source code for a simple project emphasizing the way Here is the screen for the project:
Every time you click this button, the displayed number of clicks increases. Now, let us discuss the code. As was mentioned in Part 0 of this tutorial, most of the time, we do not need to modify the App.xaml and App.xaml.cs files. So, let us take a look at the Page.xaml and Page.xaml.cs files within the solution. Here are the contents of the Page.xaml file: <UserControl x:Class="ButtonSample.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Button
x:Name="MyButton"
Width="150"
Height="25"
Content="Clicked 0 Times"/>
</Grid>
</UserControl>
We can see a simple button within a grid. The button has a name ( public partial class Page : UserControl
{
int numClicks = 0;
public Page()
{
InitializeComponent();
MyButton.Click += new RoutedEventHandler(MyButton_Click);
}
void MyButton_Click(object sender, RoutedEventArgs e)
{
numClicks++;
MyButton.Content = "Clicked " + numClicks + " times.";
}
}
Take a look at the MyButton.Click += new RoutedEventHandler(MyButton_Click);
and by creating the body of the handler as a void MyButton_Click(object sender, RoutedEventArgs e)
{
numClicks++;
MyButton.Content = “Clicked “+ numClicks + ” times.”;
}
DatePickerThe The sample allows the user to choose the date using the
Note, that in order to make xmlns:extended="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Extended"
This line makes "extended" to be the XAML namespace for the functionality located within the <extended:DatePicker
x:Name=”MyDatePicker”
HorizontalAlignment=”Center”
VerticalAlignment=”Center”>
</extended:DatePicker>
Finally, if we look at the C# code, we notice that the ListBoxThe (More complicated examples with binding will be presented in subsequent parts of this tutorial.)
Here is the interesting part of the Page.xaml file: <ListBox Width=”100″ Height=”100″>
<ListBoxItem Content=”Item1″/>
<ListBoxItem Content=”Item2″/>
<ListBoxItem Content=”Item3″/>
</ListBox>
You can see the Demo ControlsThere are many other important controls. Since I do not have time to provide examples for them all, I am going to briefly describe some of them in this section:
PanelsAs was mentioned before, panels are containers of Silverlight visual objects that have a say over where and how the objects are positioned. CanvasThe The figure below shows a
The corresponding XAML code is the following: <Canvas x:Name=”LayoutRoot” Background=”White”>
<Button
Canvas.Left=”100″
Canvas.Top=”200″
Content=”Hello World”/>
</Canvas>
As you can see, the property StackPanelThe The following figure shows the screen for the sample:
The sample includes two <StackPanel
x:Name=”LayoutRoot”
Background=”White”>
<Button
Width=”200″
Height=”25″
Margin=”0,10,0,0″
Content=”Vertical Button 1″/>
<Button
Width=”200″
Height=”25″
Margin=”0,10,0,0″
Content=”Vertical Button 2″/>
<StackPanel
Margin=”0,10,0,0″
Orientation=”Horizontal”>
<Button
Width=”150″
Height=”25″
Margin=”10,0,0,0″
Content=”Horizontal Button 1″/>
<Button
Width=”150″
Height=”25″
Margin=”10,0,0,0″
Content=”Horizontal Button 2″/>
</StackPanel>
</StackPanel>
We can see that the top level GridThe When run, this sample produces the following window:
Let us take a look at the XAML: <Grid x:Name=”LayoutRoot” Background=”White”>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”80″/>
<ColumnDefinition Width=”80″/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=”55″/>
<RowDefinition Height=”55″/>
</Grid.RowDefinitions>
<TextBlock
Grid.Row=”0″
Grid.Column=”0″
Text=”Cell (0, 0)”/>
<TextBlock
Grid.Row=”0″
Grid.Column=”1″
HorizontalAlignment=”Right”
Text=”Cell (1, 0)”/>
<TextBlock
Grid.Row=”1″
Grid.Column=”0″
Text=”Cell (0, 1)”/>
<TextBlock
Grid.Row=”1″
Grid.Column=”1″
VerticalAlignment=”Bottom”
Text=”Cell (1, 1)”/>
</Grid>
The rows and columns are defined by the ConclusionIn this part of the tutorial, we presented several Silverlight Controls and Panels, which are the building blocks of business logic applications. Later, we are going to use them to create some cooler stuff. Please rate this article if you like itIf you don't, please, give me suggestions for writing better articles. Thanks.
|
||||||||||||||||||||||