Silverlight – 如何在WP7上实现相机应用程序样式照片条?

我遇到了许多问题,造成与Camera应用程序中相片非常相似的效果.

我想做的是显示一个网格,每个网格具有与屏幕相同的尺寸(无论是纵向还是横向).已经,我不得不做一些黑客和create dependency properties that the grids width and height properties bind to维持长宽比.

这工作正常.但是当我为我的条形图创建一个StackPanel并实现我的导航(或者只是使用z-index变换缩放),我看到我的StackPanel不能显示大于屏幕尺寸(它被裁剪成只有一个网格的大小) .我以为我找到一个描述这个问题的帖子,但是现在找不到 – 如果你知道我正在考虑哪个帖子,或者如果你更多地了解这个限制,请发贴.

我发现唯一的解决方法是使用ScrollViewer,这绝对不是我想要的行为,但它允许StackPanel比屏幕更宽.

我的真正问题是ScrollViewer的行为 – 因为我需要从网格跳转到网格(就像照片条)而不是自由滚动,只要我能告诉Horizo​​ntalOffset不是一个动画的属性.我可以通过每15毫秒调用ScrollToHorizo​​ntalOffset强制它来动画,基本上是手动实现我自己的缓动效果.这似乎是一个巨大的黑客,而且这个行为是非常诡异的(或者我每次看到它都不会得到ManipulationCompleted事件) – 在每次刷卡动作结束时 – 或者内置的ScrollViewer惯性物理干扰我的效果).

有没有人知道我遇到的问题的更好的解决方法,或者完全不同的方式来获取Silverlight中相机照片条的体验?

我已经考虑过使用数据透视控件,但是它不是我想要的(如果我想让每个项目在下一个进入之前完全动画化,而不是显示为全部附加到一个条上,那么应该有更少的约束实现方式).更重要的是,该条只是我想要动态执行的许多效果之一.我想要一个CoolIris般的3D倾斜,或FlipPad风格的页面转动.我相信如果我可以让我目前的设置工作很好,这将很容易实现这些其他的效果(作为可选的转换).承诺像Pivot这样的控制不会让我更接近这个愿景.

这是我的XAML:

<Grid x:Name="LayoutRoot" Background="Transparent" Height="{Binding RealHeight,ElementName=phoneApplicationPage}" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ScrollViewer x:Name="SlideScroller" VerticalScrollBarVisibility="Disabled" Height="{Binding RealHeight,ElementName=phoneApplicationPage}" Margin="0,-31" ScrollViewer.HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Left" VerticalAlignment="Top">
            <StackPanel x:Name="SlidePanel" Orientation="Horizontal" Height="{Binding RealHeight,ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left">    
                <Grid x:Name="Slide0" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" Height="{Binding RealHeight,ElementName=phoneApplicationPage}" Background="#FFCCCCCC">
                    <Image x:Name="Photo0" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill"/>
                </Grid>
                <Grid x:Name="Slide1" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" Background="#FFCCCCCC">
                    <Image x:Name="Photo1" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill"/>
                </Grid>
                <Grid x:Name="Slide2" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" Background="#FFCCCCCC">
                    <Image x:Name="Photo2" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill"/>
                </Grid>
            </StackPanel>    
        </ScrollViewer>
    </Grid>

解决方法

事实证明,如果我只是阻止ScrollViewer被用户直接操纵并手动定位,我所描述的设置很好.这消除了我提到的造成大部分毛刺的物理效应.

XAML

<ScrollViewer x:Name="SlideScroller" VerticalScrollBarVisibility="Disabled" Height="{Binding RealHeight,ElementName=phoneApplicationPage}" ScrollViewer.HorizontalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Visible" HorizontalAlignment="Left" VerticalAlignment="Top">
            <StackPanel x:Name="SlidePanel" Orientation="Horizontal" Height="{Binding RealHeight,ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left">
            </StackPanel>
        </ScrollViewer>
<Rectangle x:Name="ScrollInterceptRect" Margin="0,-31" Width="{Binding RealWidth,ElementName=phoneApplicationPage}" HorizontalAlignment="Left" VerticalAlignment="Top">

代码隐藏

public MainPage()
    {
        InitializeComponent();

        ScrollInterceptRect.MouseLeftButtonUp += new MouseButtonEventHandler(ScrollInterceptRect_MouseLeftButtonUp);
        ScrollInterceptRect.MouseLeftButtonDown += new MouseButtonEventHandler(ScrollInterceptRect_MouseLeftButtonDown);
        ScrollInterceptRect.MouseMove += new MouseEventHandler(ScrollInterceptRect_MouseMove);
    }
    //...
    NavigationIndices navigationIndices = new NavigationIndices();
    readonly double swipeThreshold = 80.0;
    SwipeDirection swipeDirection;
    bool tapCancelled;
    Point swipeDelta;
    Point swipeStartPosition;
    double startScrollOffsetX;
    void SlideScroller_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
    {
        swipeStartPosition = e.GetPosition(this);
        startScrollOffsetX = SlideScroller.HorizontalOffset;
    }
    void ScrollInterceptRect_MouseMove(object sender,MouseEventArgs e)
    {
        Point touchPosition = e.GetPosition(this);
        swipeDelta = new Point() { X = swipeStartPosition.X - touchPosition.X,Y = swipeStartPosition.Y - touchPosition.Y };
        SlideScroller.ScrollToHorizontalOffset(startScrollOffsetX + swipeDelta.X);
        // swipe right
        if (swipeDelta.X > swipeThreshold)
        {
            swipeDirection = SwipeDirection.Left;
            tapCancelled = true;
        }
        // swipe left
        else if (swipeDelta.X < -swipeThreshold)
        {
            swipeDirection = SwipeDirection.Right;
            tapCancelled = true;
        }
    }
    void ScrollInterceptRect_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)
    {
        if (swipeDirection == SwipeDirection.Left && navigationIndices.X < photos.Count - 1 && photos[navigationIndices.X] != null)
        {
            navigationIndices.X++;
        }
        // only go back when you aren't already at the beginning
        else if (swipeDirection == SwipeDirection.Right && navigationIndices.X > 0)
        {
            navigationIndices.X--;
        }
        if (!tapCancelled)
        {
            // handle tap
        }
        else
        {
            animateScrollViewerToCurrentPhoto();
        }
    }

这简化了一点清晰度(我也使用垂直滑动在我的应用程序中的东西,我省略了我如何动画的ScrollViewer – 可能值得自己的帖子).

我很乐意听到您可以提供的任何改进,或建议更好的方法来实现它.也许扩展面板类或作为自定义行为.思考?

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...