C# 无法到达";滚动状态“;在ScrollViewer控件中

C# 无法到达";滚动状态“;在ScrollViewer控件中,c#,xaml,windows-phone-7.1,windows-phone,C#,Xaml,Windows Phone 7.1,Windows Phone,我在试着让它工作时遇到了很多麻烦,希望有人能帮我 我的WindowsPhone应用程序中有一个ScrollViewer,我正在尝试模拟一个类似于本机日历应用程序中的“日期/时间选择器”的控件。因此,我的ScrollViewer包含一个StackPanel,其中包含多个带有矩形和文本块的方形画布。我的目的是观察“滚动状态”,当VisualState更改为“NotScrolling”时,我会检查ScrollViewer的垂直偏移,并将幻灯片动画化到最近的“捕捉到”位置(即将正方形对齐到正确/中间位置

我在试着让它工作时遇到了很多麻烦,希望有人能帮我

我的WindowsPhone应用程序中有一个ScrollViewer,我正在尝试模拟一个类似于本机日历应用程序中的“日期/时间选择器”的控件。因此,我的ScrollViewer包含一个StackPanel,其中包含多个带有矩形和文本块的方形画布。我的目的是观察“滚动状态”,当VisualState更改为“NotScrolling”时,我会检查ScrollViewer的垂直偏移,并将幻灯片动画化到最近的“捕捉到”位置(即将正方形对齐到正确/中间位置)

。。。然后继续查找
VisualStateGroup=FindVisualState(元素,“滚动状态”),当事件发生更改时,他们可以从中挂钩事件

然而。。。每当我尝试将VisualTreeHelper.GetChild(sv,0)作为FrameworkElement执行时,
,应用程序就会崩溃,出现“System.ArgumentOutOfRangeException”类型的异常。如果我输出VisualTreeHelper.GetChildrenCount(sv),它总是“0”。对其他人来说,它似乎是如何起作用的?(八)

任何帮助都将不胜感激。谢谢


(作为替代方案,是否有人已经在我可以使用的可重用控件中创建了这种“选择框”,而不是尝试重新创建它?

您是否等到scrollviewer加载的事件触发后才尝试获取scrollviewer子项??

您好;是的,我应该注意到-
ScrollViewer\u Loaded()
函数-与我在上面发布的XAML中的“Loaded”事件关联-是我试图使用的
VisualTreeHelper.GetChild()
。谢谢…奇怪。看看我的代码(我有很多控件在做这件事),这一切对我来说都很好(没有像现在这样尝试过你的代码-太懒了:-)。这很奇怪。这是我正在制作的自定义用户控件的一部分,所以不是“MainPage.xaml”的一部分。。。我想可能是因为控制,但如果你和其他人这样做了,我不确定。我将用它尝试更多的东西,也许在一个干净的项目中单独尝试。(也许在这个旧项目中,WP7和7.1之间有一个步骤,我错过了一些更新?)。谢谢你的意见。
<ScrollViewer Name="sv" Width="100" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Disabled" Loaded="ScrollViewer_Loaded">
    <StackPanel>
        <Canvas MaxWidth="77" MaxHeight="80" MinWidth="80" MinHeight="80" Margin="3">
            <Rectangle Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3" Width="80" Height="80" />
            <TextBlock Text="1" FontSize="36" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Width="70" Canvas.Left="6" Canvas.Top="14" LineHeight="48" />
        </Canvas>
        <Canvas MaxWidth="77" MaxHeight="80" MinWidth="80" MinHeight="80" Margin="3">
            <Rectangle Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3" Width="80" Height="80" />
            <TextBlock Text="2" FontSize="36" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Width="70" Canvas.Left="6" Canvas.Top="14" LineHeight="48" />
        </Canvas>
        <Canvas MaxWidth="77" MaxHeight="80" MinWidth="80" MinHeight="80" Margin="3">
            <Rectangle Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3" Width="80" Height="80" />
            <TextBlock Text="3" FontSize="36" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Width="70" Canvas.Left="6" Canvas.Top="14" LineHeight="48" />
        </Canvas>
        ...
    </StackPanel>
</ScrollViewer>
// Visual States are always on the first child of the control template
FrameworkElement element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement;