Вы находитесь на странице: 1из 2

public sealed partial class BlankPage : Page

{
Random rand = new Random();
byte[] rgb = new byte[3];
public BlankPage()
{
this.InitializeComponent();
}
private void txtblk_Tapped_1(object sender, TappedRoutedEventArgs e)
{
rand.NextBytes(rgb);
Color clr = Color.FromArgb(255, rgb[0], rgb[1], rgb[2]);
txtblk.Foreground = new SolidColorBrush(clr);
}
}
I’ve removed the OnNavigatedTo method, because it’s not being used here. In the Tapped event
handler, the NextBytes method of the Random object obtains three random bytes, and these are used
to construct a Color value with the static Color.FromArgb method. The handler finishes by setting
the
Foreground property of the TextBlock to a SolidColorBrush based on that Color value.
When you run this program, you can tap the TextBlock with a finger, mouse, or stylus and it will
change to a random color. If you tap on an area of the screen outside the TextBlock, nothing
happens.
If you’re using a mouse or stylus, you might notice that you don’t need to tap the actual strokes that
comprise the letters. You can tap between and inside those strokes, and the TextBlock will still
respond.
It’s as if the TextBlock has an invisible background that encompasses the full height of the font
including diacritical marks and descenders, and that’s precisely the case.
If you look inside the BlankPage.g.cs file generated by Visual Studio, you’ll see a Connect method
containing the code that attaches the event handler to the Tapped event of the TextBlock. You can
do
this yourself in code. Try eliminating the Tapped handler assigned in the XAML file and instead
attach
an event handler in the constructor of the code-behind file:
public BlankPage()
{
this.InitializeComponent();
txtblk.Tapped += txtblk_Tapped_1;
}
No real difference.
Several properties of TextBlock need to be set properly for the Tapped event to work. The
IsHitTestVisible and IsTapEnabled properties must both be set to their default values of true. The
Visibility property must be set to its default value of Visibility.Visible. If set to Visibility.Collapsed,
the
TextBlock will not be visible at all and will not respond to user input.
The first argument to the txtblk_Tapped_1 event handler is the element that sent the event, in this
80
www.it-ebooks.infocase the TextBlock. The second argument provides information about this
particular event, including
the coordinate point at which the tap occurred, and whether the tap came from a finger, mouse, or
stylus. This information will be explored in more detail in future chapters.
Routed Event Handling
Because the first argument to the Tapped event handler is the element that generates the event, you
don’t need to give the TextBlock a name to access it from within the event handler. You can simply
cast
the sender argument to an object of type TextBlock. This is particularly useful for sharing an event
handler among multiple elements, and I’ve done precisely that in the RoutedEvents0 project.
RoutedEvents0 is the first of several projects that demonstrate the concept of routed event handling,
which is an important feature of the Windows Runtime. But this particular program doesn’t show
any
features particular to routed events. Hence the suffix of zero. For this project I created the Tapped
handler first with the proper signature and my preferred name:
Project: RoutedEvents0 | File: BlankPage.xaml.cs (excerpt)
public sealed partial class BlankPage : Page
{
Random rand = new Random();
byte[] rgb = new byte[3];
public BlankPage()
{
this.InitializeComponent();
}
void OnTextBlockTapped(object sender, TappedRoutedEventArgs args)
{
TextBlock txtblk = sender as TextBlock;
rand.NextBytes(rgb);
Color clr = Color.FromArgb(255, rgb[0], rgb[1], rgb[2]);
txtblk.Foreground = new SolidColorBrush(clr);
}
}
Notice the first line of the event handler casts the sender argument to TextBlock.
Because this event handler already exists in the code-behind file, Visual Studio suggests that name
when you type the name of the event in the XAML file. This was handy because I added nine
TextBlock
elements to the Grid:
Project: RoutedEvents0 | File: BlankPage.xaml (excerpt)
<Page
x:Class="RoutedEvents0.BlankPage"
...
FontSize="48">
81

Вам также может понравиться