问题
I would like to make a horizontal listbox that could be infinite scrolling (looping listbox/scrollviewer, or circular listbox/scrollviewer), means that when I scroll to its end, its would scroll over from beginning.
I have tried to make the listbox horizontal, but it wouldn't scroll so I put it inside a scrollviewer to have it scroll horizontally. However it's not the listbox that scroll and I need them somehow to scroll over from beginning.
This is what I have so far:
private void DoWebClient()
{
var webClient = new WebClient();
webClient.OpenReadAsync(new Uri("http://music.mobion.vn/api/v1/music/userstop?devid="));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
using (var reader = new StreamReader(e.Result))
{
string s = reader.ReadToEnd();
Stream str = e.Result;
str.Position = 0;
XDocument xdoc = XDocument.Load(str);
var data = from query in xdoc.Descendants("user")
select new mobion
{
avlink = (string)query.Element("user_info").Element("avlink"),
nickname = (string)query.Element("user_info").Element("nickname"),
track = (string)query.Element("track"),
artist = (string)query.Element("artist"),
};
listBox.ItemsSource = data;
}
}
Mainpage.xaml
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid/>
</DataTemplate>
<Storyboard x:Name="Storyboard1" RepeatBehavior="forever" Completed="Storyboard1_Completed">
<DoubleAnimation Duration="0:0:25" To="-2400" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="imagesScrollview" d:IsOptimized="True"/>
</Storyboard>
</phone:PhoneApplicationPage.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="8,563,-2400,2" Width="auto" x:Name="imagesScrollview" Opacity="1" Background="#FF3ED216" Grid.Row="1" RenderTransformOrigin="0.5,0.5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<im:ControlStoryboardAction Storyboard="{StaticResource Storyboard1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ScrollViewer.RenderTransform>
<CompositeTransform/>
</ScrollViewer.RenderTransform>
<ListBox x:Name="listBox" Width="Auto" Height="Auto" Background="#FF3ED216" ManipulationCompleted="listBox_ManipulationCompleted">
<ListBox.RenderTransform>
<CompositeTransform/>
</ListBox.RenderTransform>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.RenderTransform>
<TranslateTransform X="0" />
</StackPanel.RenderTransform>
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="15,0">
<Image Name="imageAV" Source="{Binding Path=avlink}" Height="80" Width="80" Stretch="UniformToFill" MouseLeftButtonUp="imageAV_MouseLeftButtonUp" ImageFailed="imageAV_ImageFailed" />
<StackPanel Orientation="Vertical" Margin="10,0,0,0" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">
<TextBlock Name="indexTextBlock" Text="{Binding num}" />
<TextBlock Text="{Binding nickname}"/>
<TextBlock Text="{Binding track}" FontWeight="Bold" />
<TextBlock Text="{Binding artist}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
回答1:
To make a listbox scroll horizontally you need to set the ScrollBar Visibility properties of the internal ScrollViewer.
Like this:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Disabled" >
The Toolkit includes a LoopingSelector
class that you could based your own control implementation on. There's an example of creating such a control at http://babaandthepigman.wordpress.com/2010/09/22/wp7-looping-selector/ (although it is for a vertical list)
回答2:
Not sure if that helps, but the standard approach to infinite scrolling is to put at least one screenful of duplicate items from the beginning of the list at the end, and upon reaching the end, transparently skip (1-step scroll) to the beginning or a fixed short offset from it (and vice versa) so that it looks the same but the user looks at the items from the beginning of the list, not their duplicates from the end.
来源:https://stackoverflow.com/questions/6675267/horizontal-listbox-that-could-be-infinite-circle-scrolling