在ScrollViewer中使用画布的Windows Phone 8.1 UserControl不起作用

时间:2015-04-08 11:21:53

标签: canvas windows-phone-8.1 scrollviewer

我在用户控件中有以下内容:

<Grid Background="#FFEEEEEE" Margin="0,0,0,0">
    <TextBlock x:Name="TitleLabel" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Foreground="Black" FontSize="16" FontWeight="Bold"/>

    <Border BorderBrush="#FFE2E2E2" Margin="10,60,20,80" CornerRadius="4" BorderThickness="2">
        <Grid Background="#ffffffff">
            <Grid x:Name="SignatureGrid" Background="Transparent">
                <TextBlock TextWrapping="Wrap" Text="Sign here" Foreground="#FFEEEEEE" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="100"/>
                <Border BorderBrush="#FF333333" BorderThickness="1" Height="1" VerticalAlignment="Bottom" Margin="20,0,20,50" />
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Sign here" VerticalAlignment="Bottom" Foreground="#FF333333" FontSize="14" Margin="20,0,0,27"/>
            </Grid>

            <Canvas x:Name="inkCanvas" Background="Transparent" PointerPressed="inkCanvas_PointerPressed" PointerMoved="inkCanvas_PointerMoved" PointerReleased="inkCanvas_PointerReleased" />
        </Grid>
    </Border>

    <Button x:Name="ClearButton" Content="Clear" HorizontalAlignment="Right" Margin="0,0,20,20" VerticalAlignment="Bottom" Width="162" Background="#FF46B2D5" BorderThickness="0,0,2,0" Click="Button_Click"/>
</Grid>

然后我有一个包含以下内容的页面:

<ScrollViewer Grid.Row="2" x:Name="QuestionsScroller" HorizontalAlignment="Stretch" ZoomMode="Disabled" HorizontalScrollMode="Disabled" >
        <StackPanel x:Name="SectionContentPanel" />
    </ScrollViewer>

在后面的页面代码中,我动态地将控件(和其他控件)添加到stackpanel。在它的当前形式中,画布不起作用,但如果我删除ScrollViewer,画布效果很好,但我显然无法滚动到它下面的控件。

在我的PointerMoved事件中,我有:

Point currentPoint = e.GetCurrentPoint(inkCanvas).Position;
if (_startPoint != currentPoint)
{
    _line.Points.Add(currentPoint);
    mHasDrawing = true;
}

使用ScrollViewer,起始点和端点永远不会不同(我猜是因为当我在屏幕/模拟器上拖动时,画布正在用我的手指/鼠标移动。没有ScrollView,正如我所说,它按预期工作。

所以问题是,如何在scrollViewer中实现Canvas?

1 个答案:

答案 0 :(得分:0)

在搞乱了一段时间之后,我在StackOverflow上问了一个问题,然后在10分钟内我明白了 - 这是任何想要做同样事情的人的答案:

在PointerPressed事件中,您需要禁用scrollViewer滚动模式:

private void inkCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
    {            
        //get to the scrollViewer
        StackPanel sp = (StackPanel)this.Parent;
        ScrollViewer sv = (ScrollViewer)sp.Parent;
        sv.VerticalScrollMode = ScrollMode.Disabled;

        //Rest of code.
    }

此时Canvas可以正常工作,但您仍然无法滚动,直到再次启用ScrollViewer。您可以在PointerReleased事件中执行此操作:

private void inkCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        //get to the scrollViewer
        StackPanel sp = (StackPanel)this.Parent;
        ScrollViewer sv = (ScrollViewer)sp.Parent;
        sv.VerticalScrollMode = ScrollMode.Auto;

        //Rest of code
    }