1

我创建水平滑动会在下一个和上一个项目之间移动。我有三个图像,但问题是当我轻弹一次时它直接转到第三个图像,所以剩下第二个图像。

参考我的 C# 代码如下:

<toolkit:GestureService.GestureListener>
            <toolkit:GestureListener Flick="OnFlick"/>
        </toolkit:GestureService.GestureListener>


 private void OnFlick(object sender, FlickGestureEventArgs e)
        {
            try
            {

                    double ScreenWidth = ScrollGrid.Width;
                    // User flicked towards left
                    if (e.HorizontalVelocity < 0)
                    {

                        //Load Next Page                
                        double nextPage = (ScrollActivePage + 1) * ScreenWidth;
                        if (nextPage - ScrollGrid.ScrollableWidth <= ScreenWidth)
                        {
                            ScrollGrid.ScrollToHorizontalOffset(nextPage);
                            ScrollActivePage++;
                        }
                        else
                        {
                        ScrollGrid.ScrollToHorizontalOffset(ScrollGrid.ScrollableWidth);

                         }     
                    }


                    // User flicked towards right
                    if (e.HorizontalVelocity > 0)
                    {
                        //Load Previous Page;                
                        ScrollActivePage = (ScrollActivePage > 0) ? ScrollActivePage - 1 : 0;
                        ScrollGrid.ScrollToHorizontalOffset(ScrollActivePage * ScrollGrid.Width);


                    }


                }
4

1 回答 1

2

我假设您显示的图像都是相同的宽度,我会建议您使用一种相当简单但不同的方法来解决您的问题。

首先像这样定义你的网格

<Grid x:Name="ScrollGrid" Width="1500">
    <Grid.RenderTransform>
        <TranslateTransform x:Name="gridTransform"/>
    </Grid.RenderTransform>
    <!-- grid components or images -->
</Grid>

然后在 C# 代码中的 OnFlick() 方法中使用以下两个方法:

private void OnFlick(object sender, FlickGestureEventArgs e)
{
        try
        {
            // User flicked towards left
            if (e.HorizontalVelocity < 0)
            {
                //Load Next Page
                swipeLeft();
            }

            // User flicked towards right
            if (e.HorizontalVelocity > 0)
            {
                //Load Previous Page;                
                swipeRight();
            }
}

private void swipeLeft()
    {
        //Animate the Grid to the left side
        DoubleAnimation da = new DoubleAnimation();
        da.From = gridTransform.X;
        da.To = gridTransform.X - 360; //You can customize this 360 to your image width by passing it as an argument to swipeLeft method
        da.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        Storyboard.SetTarget(da, gridTransform);
        Storyboard.SetTargetProperty(da, new PropertyPath(TranslateTransform.XProperty));

        Storyboard sb1 = new Storyboard();
        sb1.Children.Add(da);
        sb1.Begin();
    }

    private void swipeRight()
    {
        //Animate the Grid to the right side
        DoubleAnimation da = new DoubleAnimation();
        da.From = gridTransform.X;
        da.To = gridTransform.X + 360;
        da.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        Storyboard.SetTarget(da, gridTransform);
        Storyboard.SetTargetProperty(da, new PropertyPath(TranslateTransform.XProperty));

        Storyboard sb1 = new Storyboard();
        sb1.Children.Add(da);
        sb1.Begin();
    }

我为我的项目编写了这些方法,可能需要根据您的要求进行一些自定义。但值得一试且易于修改的代码。

//还要确保 ScrollGrid 的任何父级没有覆盖或限制 ScrollGrid 的宽度

于 2012-08-21T13:20:08.923 回答