点击时文本块数组获取文本
Textblock array get Text when tapped
我创建了一个文本块数组(用于创建键盘)。我想在点击一个文本块时获得文本块文本。
keyboardButtons[i] = new TextBlock();
keyboardButtons[i].Width = 45;
keyboardButtons[i].Height = 55;
keyboardButtons[i].Text = alphabet.Substring(i, 1);
keyboardButtons[i].Foreground = new SolidColorBrush(Colors.White);
keyboardButtons[i].FontSize = 25.0;
keyboardButtons[i].TextAlignment = TextAlignment.Center;
Canvas.SetLeft(keyboardButtons[i], xPos);
Canvas.SetTop(keyboardButtons[i], yPos);
RectCanvas.Children.Add(keyboardButtons[i]);
keyboardButtons[i].Tapped += new TappedEventHandler(ButtonTap);
点击方法:
void ButtonTap(object sender, TappedRoutedEventArgs e)
{
}
ButtonTap方法应该写什么?
尝试将 sender 转换为控件并获取其文本 属性
var textBlock = sender as TextBlock;
if(textBlock!=null) {
var text = textBlock.Text;
}
A 按钮控件 是更好的选择。您可以将字母作为 Button 控件的 TAG 属性 传递。
下图显示按钮 B 被按下(仅作为示例)。您可以随时向其中添加自己的逻辑。
C# 文件背后的代码
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
char Key = 'A';
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Tag = Key + i;
button.FontSize = 40;
button.Width = 100;
button.Height = 100;
button.Content = (char)(Key + i);
button.Click += button_Click;
stackPanel1.Children.Add(button);
}
for (int i = 10; i < 20; i++)
{
Button button = new Button();
button.Tag = Key + i;
button.FontSize = 40;
button.Width = 100;
button.Height = 100;
button.Content = (char)(Key + i);
button.Click += button_Click;
stackPanel2.Children.Add(button);
}
for (int i = 20; i < 26; i++)
{
Button button = new Button();
button.Tag = Key + i;
button.FontSize = 40;
button.Width = 100;
button.Height = 100;
button.Content = (char)(Key + i);
button.Click += button_Click;
stackPanel3.Children.Add(button);
}
}
//Now use a switch case to get the TAG property
private void button_Click(object sender, RoutedEventArgs e)
{
var t = (sender as Button).Tag;
char tag = Convert.ToChar(t);
switch (tag)
{
case 'A':
console.Text = "You typed " + tag;
//Your code logic
break;
case 'B':
console.Text = "You typed " + tag;
//Your code logic
break;
case 'C':
console.Text = "You typed " + tag;
//Your code logic
break;
//Rest of the alphabets
}
}
XAML 文件
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="150"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="40, 200, 20, 20">
<StackPanel x:Name="stackPanel1" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="stackPanel2" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="stackPanel3" Orientation="Horizontal">
</StackPanel>
</StackPanel>
<TextBox x:Name="console" Height="100" Width="1000" Grid.Row="1" FontSize="50" Text="Press a Button"/>
</Grid>
我创建了一个文本块数组(用于创建键盘)。我想在点击一个文本块时获得文本块文本。
keyboardButtons[i] = new TextBlock();
keyboardButtons[i].Width = 45;
keyboardButtons[i].Height = 55;
keyboardButtons[i].Text = alphabet.Substring(i, 1);
keyboardButtons[i].Foreground = new SolidColorBrush(Colors.White);
keyboardButtons[i].FontSize = 25.0;
keyboardButtons[i].TextAlignment = TextAlignment.Center;
Canvas.SetLeft(keyboardButtons[i], xPos);
Canvas.SetTop(keyboardButtons[i], yPos);
RectCanvas.Children.Add(keyboardButtons[i]);
keyboardButtons[i].Tapped += new TappedEventHandler(ButtonTap);
点击方法:
void ButtonTap(object sender, TappedRoutedEventArgs e)
{
}
ButtonTap方法应该写什么?
尝试将 sender 转换为控件并获取其文本 属性
var textBlock = sender as TextBlock;
if(textBlock!=null) {
var text = textBlock.Text;
}
A 按钮控件 是更好的选择。您可以将字母作为 Button 控件的 TAG 属性 传递。
下图显示按钮 B 被按下(仅作为示例)。您可以随时向其中添加自己的逻辑。
C# 文件背后的代码
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
char Key = 'A';
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Tag = Key + i;
button.FontSize = 40;
button.Width = 100;
button.Height = 100;
button.Content = (char)(Key + i);
button.Click += button_Click;
stackPanel1.Children.Add(button);
}
for (int i = 10; i < 20; i++)
{
Button button = new Button();
button.Tag = Key + i;
button.FontSize = 40;
button.Width = 100;
button.Height = 100;
button.Content = (char)(Key + i);
button.Click += button_Click;
stackPanel2.Children.Add(button);
}
for (int i = 20; i < 26; i++)
{
Button button = new Button();
button.Tag = Key + i;
button.FontSize = 40;
button.Width = 100;
button.Height = 100;
button.Content = (char)(Key + i);
button.Click += button_Click;
stackPanel3.Children.Add(button);
}
}
//Now use a switch case to get the TAG property
private void button_Click(object sender, RoutedEventArgs e)
{
var t = (sender as Button).Tag;
char tag = Convert.ToChar(t);
switch (tag)
{
case 'A':
console.Text = "You typed " + tag;
//Your code logic
break;
case 'B':
console.Text = "You typed " + tag;
//Your code logic
break;
case 'C':
console.Text = "You typed " + tag;
//Your code logic
break;
//Rest of the alphabets
}
}
XAML 文件
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="150"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="40, 200, 20, 20">
<StackPanel x:Name="stackPanel1" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="stackPanel2" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="stackPanel3" Orientation="Horizontal">
</StackPanel>
</StackPanel>
<TextBox x:Name="console" Height="100" Width="1000" Grid.Row="1" FontSize="50" Text="Press a Button"/>
</Grid>