1

这是一个适用于 Windows Phone 8 的项目。

我尝试使用 XamlReader 加载包含多个对象的 Grid,其中一个是按钮。该按钮可以在 c# 中很好地创建、找到并附加一个处理程序,但似乎没有响应点击 - 按下时按钮不会改变颜色,并且没有调用关联的处理程序。

XamlReader 正在读取的 XAML 是

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Name = "testgrid" Background = "Transparent">
        <Button  x:Name="btn" Content="Button" HorizontalAlignment="Left" 
          VerticalAlignment="Top" Margin="0,0,0,0" Grid.Row="1" Height="81"/>
        <TextBlock Text = "Test"/>
</Grid>

使用的c#代码是

Grid tGrid = (Grid)XamlReader.Load(xaml);
Button tgtButton = (Button)tGrid.FindName("btn");      
LayoutRoot.Children.Add((UIElement)tGrid);
tgtButton.Content = "bloop";

这里可能是什么问题?谢谢!

ps 如果加载的 xaml 只是一个按钮,使用相同的方法将其添加到 LayoutRoot,它会响应按下。

4

1 回答 1

0

一个非常非常基本的错误 - 我忽略了通过不定义网格行来正确安排控件。TextBlock 位于 Button 的顶部 - 即使文本没有完全覆盖该区域,它的透明部分似乎仍然挡住了按钮。吸取的教训是这与逻辑树或初始化无关!

于 2014-05-13T15:35:34.603 回答