2013-01-09 214 views
1

我試圖在Windows Phone 7.1項目中實現「無限滾動」。無限滾動 - Windows Phone 7/8

大多數其他帖子指向此MSDN Blog link

雖然我正在努力實現這一點,因爲它並沒有真正放棄它的工作方式或代碼放置位置。另外,當我粘貼代碼時,它似乎缺少空格,所以我已經完成了代碼並添加了缺失的空格,並根據我的知識進行了編輯。

我已經添加了XAML代碼到App.xaml<Application.Resources>這是正確的,這是我App.xaml文件的內容:

<Application 
    x:Class="ScrollWindowBottom.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"> 

    <!--Application Resources--> 
    <Application.Resources> 

     <Style TargetType="ScrollViewer"> 
      <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> 
      <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="Padding" Value="0"/> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ScrollViewer"> 
         <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
Background="{TemplateBinding Background}"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="ScrollStates"> 
            <VisualStateGroup.Transitions> 
             <VisualTransition GeneratedDuration="00:00:00.5"/> 
            </VisualStateGroup.Transitions> 
            <VisualState x:Name="Scrolling"> 
             <Storyboard> 
              <DoubleAnimation Storyboard.TargetName="VerticalScrollBar" 
Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
              <DoubleAnimation Storyboard.TargetName="HorizontalScrollBar" 
Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="NotScrolling"> 
            </VisualState> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="VerticalCompression"> 
            <VisualState x:Name="NoVerticalCompression"/> 
            <VisualState x:Name="CompressionTop"/> 
            <VisualState x:Name="CompressionBottom"/> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="HorizontalCompression"> 
            <VisualState x:Name="NoHorizontalCompression"/> 
            <VisualState x:Name="CompressionLeft"/> 
            <VisualState x:Name="CompressionRight"/> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Grid Margin="{TemplateBinding Padding}"> 
           <ScrollContentPresenter x:Name="ScrollContentPresenter" Content="{TemplateBinding Content}" 
ContentTemplate="{TemplateBinding ContentTemplate}"/> 
           <ScrollBar x:Name="VerticalScrollBar" IsHitTestVisible="False" Height="Auto" Width="5" 
HorizontalAlignment="Right" VerticalAlignment="Stretch" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{TemplateBinding VerticalOffset}" 
Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" /> 
           <ScrollBar x:Name="HorizontalScrollBar" IsHitTestVisible="False" Width="Auto" Height="5" 
HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" 
IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{TemplateBinding HorizontalOffset}" 
Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" /> 
          </Grid> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

    </Application.Resources> 

    <Application.ApplicationLifetimeObjects> 
     <!--Required object that handles lifetime events for the application--> 
     <shell:PhoneApplicationService 
      Launching="Application_Launching" Closing="Application_Closing" 
      Activated="Application_Activated" Deactivated="Application_Deactivated"/> 
    </Application.ApplicationLifetimeObjects> 

</Application> 

這是我的MainPage.xaml.cs文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 

namespace ScrollWindowBottom 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     if(!App.ViewModel.IsDataLoaded) 
     { 
      App.ViewModel.LoadData(); 
     } 

     if(alreadyHookedScrollEvents) 
      return; 

     alreadyHookedScrollEvents = true; 
     MainListBox.AddHandler(ListBox.ManipulationCompletedEvent, (EventHandler<ManipulationCompletedEventArgs>)LB_ManipulationCompleted, true); 
     sb = (ScrollBar)FindElementRecursive(MainListBox, typeof(ScrollBar)); 
     sv = (ScrollViewer)FindElementRecursive(MainListBox, typeof(ScrollViewer)); 

     if(sv != null) 
     { 
      // Visual States are always on the first child of the control template 
      FrameworkElement element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement; 
      if(element != null) 
      { 
       VisualStateGroup group = FindVisualState(element, "ScrollStates"); 
       if(group != null) 
       { 
        group.CurrentStateChanging += newEventHandler<VisualStateChangedEventArgs>(group_CurrentStateChanging); 
       } 
       VisualStateGroup vgroup = FindVisualState(element, "VerticalCompression"); 
       VisualStateGroup hgroup = FindVisualState(element, "HorizontalCompression"); 
       if(vgroup != null) 
       { 
        vgroup.CurrentStateChanging += newEventHandler<VisualStateChangedEventArgs>(vgroup_CurrentStateChanging); 
       } 
       if(hgroup != null) 
       { 
        hgroup.CurrentStateChanging += newEventHandler<VisualStateChangedEventArgs>(hgroup_CurrentStateChanging); 
       } 
      } 
     }   

    } 

    private UIElement FindElementRecursive(FrameworkElement parent, Type targetType) 
     { 
      int childCount = VisualTreeHelper.GetChildrenCount(parent); 
      UIElement returnElement = null; 
      if (childCount > 0) 
      { 
       for (int i = 0; i < childCount; i++) 
       { 
        Object element = VisualTreeHelper.GetChild(parent, i); 
        if (element.GetType() == targetType) 
        { 
         return element as UIElement; 
        } 
        else 
        { 
         returnElement = FindElementRecursive(VisualTreeHelper.GetChild(parent, i) as FrameworkElement, targetType); 
        } 
       } 
      } 
      return returnElement; 
     } 


     private VisualStateGroup FindVisualState(FrameworkElement element, string name) 
     { 
      if (element == null) 
       return null; 

      IList groups = VisualStateManager.GetVisualStateGroups(element); 
      foreach (VisualStateGroup group in groups) 
       if (group.Name == name) 
        return group; 

      return null; 
     } 
    } 
} 

有了這些兩塊代碼到位時,當我嘗試運行我的應用程序到模擬器時,我只是得到一個錯誤加載:

enter image description here

+0

我們需要查看您的xaml。 –

+0

對不起,這是來自MSDN博客的XAML。我會把它放在那裏。 – Luke

+0

我已經添加了我的'App.xaml'和'MainPage.xaml.cs'的全部內容。 – Luke

回答

1

看起來您缺少所需的主要XAML,即。 MainPage.xaml上的MainListBox元素。您已添加引用該文章的代碼,但實際上並未在您嘗試使用它的頁面上獲得ListBox

我會建議從鏈接的MSDN博客文章中下載ZIP文件,並查看完整示例以查看您需要的代碼。

還請注意,此解決方案是較舊的方法,現在推薦使用LongListSelector的新方法。有關更多參考資料,請參閱this blog post from Microsoft on LongListSelector以及Windows Phone Toolkit以獲取您自己的項目的LongListSelector(請注意,它本身包含在Windows Phone 8中)。

+0

謝謝Rudi,我會看看'LongListSelector「! – Luke