XAML中的Silverlight控件在运行时为空

时间:2009-02-23 22:09:06

标签: silverlight initialization nullreferenceexception

我已经创建了一个带有DeepEarth地图控件的silverlight应用程序。这是我的基础XAML

<Grid x:Name="LayoutRoot" >
    <!-- Map Control -->
    <DeepEarth:Map x:Name="map" Margin="0,0,0,0">
        <Controls:CoordControl VerticalAlignment="Bottom" HorizontalAlignment="Right" />
        <Controls:ScaleControl VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="12,12,12,40" />
    </DeepEarth:Map>
    <Controls:NavControl x:Name="navControl" >
        <StackPanel Orientation="Horizontal">
            <Button x:Name="MyButton" Content="Hi" Click="MyButton_Click"></Button>
        </StackPanel>
    </Controls:NavControl>
</Grid>

我有一个我创建的行为类,我想附加到按钮

public Page()
{
    InitializeComponent();
    new ButtonActionWithCancel(MyButton, new CreatePointCommand(map));
}
void MyButton_Click(object sender, RoutedEventArgs e)
{
    if (MyButton == null) throw new Exception("Wat?");
}

这是为了向MyButton做一些事情,挂钩一些事件和东西。问题是MyButton在运行时为空。怎么会?层次结构中的其他控件不为null。怎么回事?

1 个答案:

答案 0 :(得分:1)

当我离开测试版时,它停止了发生。