简体   繁体   English

WP7动态枢轴

[英]WP7 dynamic pivot

I'm having trouble implementing a dynamic pivot control. 我在实现动态枢轴控制方面遇到了麻烦。 What I would like to do, is create an image gallery using the pivot, where you could change the image by swiping. 我想做的是使用枢轴创建一个图像库,您可以通过滑动更改图像。 This works natively with the pivot control by binding it's itemsource on my observable collection to display one image on each pivotitem. 这通过在我的可观察集合上绑定它的itemsource以在每个pivotitem上显示一个图像,本身与枢轴控件一起工作。 The thing is that this solution takes a lot of memory when my gallery contains more than 10 pictures, as it creates an equal number of pivotitems. 问题是,当我的图库包含10张以上的图片时,这个解决方案占用了大量内存,因为它创建了相同数量的pivotitems。

What I tried is initializing my collection with 3 items, the currently displayed one, the previous and the next one, and when the user swipes, I updated my list. 我尝试的是用3个项目初始化我的集合,当前显示的项目,前一个和下一个项目,当用户滑动时,我更新了我的列表。 I tried this using the "SelectionChanged" event and also with the gesture listener.. without success so far. 我尝试使用“SelectionChanged”事件以及手势监听器...到目前为止没有成功。 Has anyone ever tried to do this? 有没有人试过这样做? And if yes, how can I implement it? 如果是,我该如何实施呢?

I do something similar, but don't use a Pivot control. 我做了类似的事情,但不使用Pivot控件。 Download the Silverlight toolkit (http://silverlight.codeplex.com/) and use a GestureListener to catch the swipe action: 下载Silverlight工具包(http://silverlight.codeplex.com/)并使用GestureListener捕获滑动操作:

<toolkit:GestureService.GestureListener>
        <toolkit:GestureListener DragCompleted="OnDrag"/>
</toolkit:GestureService.GestureListener>

Then in code, you have an event handler that catches the swipe event, and updates the image accordingly. 然后在代码中,您有一个捕获滑动事件的事件处理程序,并相应地更新图像。 Here is the event handler that I use to change my page: 这是我用来更改页面的事件处理程序:

private void OnDrag(object sender, DragCompletedGestureEventArgs e)
{
    if (e.Direction == System.Windows.Controls.Orientation.Horizontal)
    {
        if (e.HorizontalChange < 0)
        {
            TurnPagesForward(1);
        }
        else
        {
            TurnPagesBackward(1);
        }
    }
}

If you update the items in your list your binding will be lost, so this will probably not work very well. 如果更新列表中的项目,则绑定将丢失,因此这可能无法正常工作。

  • What might work is the following (but I have not tested it): 可能有用的是以下内容(但我没有测试过):
  • Start with an initial collection with five items. 从包含五个项目的初始集合开始。
  • Make sure you display the third item in the list so that the visible item is in the middle of the list. 确保在列表中显示第三个项目,以便可见项目位于列表的中间。
  • When changing the visible page the SelectionChanged event is raised. 更改可见页面时,会引发SelectionChanged事件。
  • If you move to the right the selected index is changed to the fourth element. 如果向右移动,则所选索引将更改为第四个元素。 In this case you remove the first element in the list and add a new fifth element. 在这种情况下,您将删除列表中的第一个元素并添加新的第五个元素。
  • If you move to the left you do the opposite and remove the last element in the list. 如果向左移动,则执行相反操作并删除列表中的最后一个元素。

This should ensure that you always can move back and forth while not removing or adding any visible items. 这应确保您始终可以在不移除或添加任何可见项目的情况下来回移动。

I am not sure if this will work, but it is worth a try. 我不确定这是否有效,但值得一试。 :-) :-)

Don't do this. 不要这样做。 Lots of people have tried and it doesn't scale to large numbers of images. 很多人都尝试过,它不能扩展到大量的图像。

Although it would be really nice if it was possible to use the pivot control to create a simple image navigation control it just isn't practical. 虽然如果可以使用枢轴控制来创建简单的图像导航控件真的很好,但这是不切实际的。 You'll need to go the more complicated route of doing this yourself with a different method. 您需要使用不同的方法自行完成更复杂的路径。 (Lots of people have tried different ways. Go with what's best for your needs/experience/preferences.) (许多人尝试过不同的方式。根据您的需求/经验/偏好去做。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM