我有一些矩形,当你双击它们所在的屏幕时,我想让它"出现":
<StackPanel>
<Rectangle Name="MyRect1" Opacity="0" Height="100" DoubleTapped="MyRect1_DoubleTapped" Fill="Aqua" />
<Rectangle Name="MyRect2" Opacity="0" Height="100" DoubleTapped="MyRect2_DoubleTapped" Fill="Black"/>
<Rectangle Name="MyRect3" Opacity="0" Height="100" DoubleTapped="MyRect3_DoubleTapped" Fill="Blue"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我目前的代码背后 - 这有效:
private void MyRect1_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
MyRect1.Opacity = 1;
}
private void MyRect2_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
MyRect2.Opacity = 1;
}
private void MyRect3_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
MyRect3.Opacity = 1;
}
Run Code Online (Sandbox Code Playgroud)
由于我可能需要十几个或更多这些出现的矩形,有没有办法只为所有这些使用一个事件处理程序?
即,而不是为每个矩形都有一个处理程序,有类似的东西:
<StackPanel>
<Rectangle Name="MyRect1" Opacity="0" Height="100" DoubleTapped="MyRect_DoubleTapped" Fill="Aqua" />
<Rectangle Name="MyRect2" Opacity="0" Height="100" DoubleTapped="MyRect_DoubleTapped" Fill="Black"/>
<Rectangle Name="MyRect3" Opacity="0" Height="100" DoubleTapped="MyRect_DoubleTapped" Fill="Blue"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
使用单个处理程序MyRect_DoubleTapped:
private void MyRect_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
(some way to tell which rectangle was DoubleTapped)
(the identified rectangle).Opacity = 1;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用它sender来知道哪一个被轻敲
private void MyRect_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
Rectangle myRect = (Rectangle)sender;
myRect.Opacity = 1;
}
Run Code Online (Sandbox Code Playgroud)
这sender是引发事件的对象.
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |