2012-02-14 202 views
0

我在Windows Phone 7頁面的內容面板網格周圍實現了ScrollViewer。但是,每當我嘗試向下滾動到頁面底部時,只要我將手指從屏幕上移開,它就會立即回到原始位置。換句話說,滾動它並不保留它的最後位置,使滾動毫無意義。如何阻止頁面回彈到原始位置

這是我的網頁代碼:

<phone:PhoneApplicationPage 
         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" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71" x:Class="StepsForWater.Views.Submit" 
         mc:Ignorable="d" 
         d:DesignWidth="480" 
         d:DesignHeight="696" 
         FontFamily="{StaticResource PhoneFontFamilyNormal}" 
         FontSize="{StaticResource PhoneFontSizeNormal}" 
         Foreground="{StaticResource PhoneForegroundBrush}" 
         SupportedOrientations="Portrait" 
         Orientation="Portrait" 
         shell:SystemTray.IsVisible="True" 
         DataContext="{Binding Submit, Source={StaticResource Locator}}"> 

<!-- Sample code showing usage of ApplicationBar --> 
<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton x:Name="appbar_About" IconUri="/Images/appbar_button1.png" Text="About" Click="appbar_About_Click"/> 
     <shell:ApplicationBarIconButton x:Name="appbar_WalkRun" IconUri="/Images/appbar_button2.png" Text="Walk/Run" Click="appbar_WalkRun_Click"/>    
     <!--<shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"></shell:ApplicationBarMenuItem> 
      <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"></shell:ApplicationBarMenuItem> 
     </shell:ApplicationBar.MenuItems>--> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

<Grid x:Name="LayoutRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

    <Grid.Background> 
     <ImageBrush ImageSource="/StepsForWater;component/Images/submit-bg.png" /> 
    </Grid.Background> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" 
       Grid.Row="0" 
       Margin="12,17,0,28"> 

     <!--<TextBlock x:Name="ApplicationTitle" 
        Text="MY APPLICATION" 
        Style="{StaticResource PhoneTextNormalStyle}" /> 
     <TextBlock x:Name="PageTitle" 
        Text="page name" 
        Margin="9,-7,0,0" 
        Style="{StaticResource PhoneTextTitle1Style}" />--> 
    </StackPanel> 

    <ScrollViewer> 
    <!--ContentPanel - place additional content here-->   
    <Grid x:Name="ContentPanel" 
      Grid.Row="1" 
      Margin="12,0,12,-241" Background="{x:Null}">    
     <TextBlock Height="30" HorizontalAlignment="Left" Margin="38,57,0,0" x:Name="tBlk_Username" Text="{Binding UserName}" VerticalAlignment="Top" /> 
     <TextBox Height="72" HorizontalAlignment="Left" Margin="27,79,0,0" x:Name="tb_UserNameVal" Text="{Binding UserNameValue, Mode=TwoWay}" VerticalAlignment="Top" Width="423" /> 
     <TextBlock Height="30" HorizontalAlignment="Left" Margin="38,169,0,0" x:Name="tBlk_Email" Text="{Binding Email}" VerticalAlignment="Top" /> 
     <TextBlock Height="30" HorizontalAlignment="Left" Margin="38,278,0,0" x:Name="tBlk_Message" Text="{Binding Message}" VerticalAlignment="Top" /> 
     <TextBox Height="72" HorizontalAlignment="Left" Margin="27,191,0,0" x:Name="tb_EmailVal" Text="{Binding EmailValue, Mode=TwoWay}" VerticalAlignment="Top" Width="423" /> 
     <TextBox Height="153" HorizontalAlignment="Left" Margin="27,300,0,0" x:Name="tb_MessageVal" Text="{Binding MessageValue, Mode=TwoWay}" VerticalAlignment="Top" Width="423" /> 
     <CheckBox Content="{Binding Location}" Height="72" HorizontalAlignment="Left" Margin="27,459,0,0" x:Name="chk_Location" VerticalAlignment="Top" /> 
     <Button Content="{Binding Submit}" Height="72" HorizontalAlignment="Left" Margin="27,761,0,0" x:Name="btn_Submit" VerticalAlignment="Top" Width="160" > 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <GalaSoft_MvvmLight_Command:EventToCommand x:Name="SubmitClick" Command="{Binding SubmitCommand}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button>    
     <TextBlock Height="30" HorizontalAlignment="Left" Margin="50,540,0,0" Name="tBlk_PicInfo" Text="{Binding PictureInfo}" VerticalAlignment="Top" Grid.Row="1" /> 
     <Image Height="150" HorizontalAlignment="Left" Margin="50,583,0,0" Name="img_FlickrPic" Stretch="Fill" VerticalAlignment="Top" Width="200" Grid.Row="1" Source="{Binding ImageSource}"/> 
     <Button Content="Capture" Height="72" HorizontalAlignment="Left" Margin="267,0,0,138" Name="btn_Capture" VerticalAlignment="Bottom" Width="160" d:LayoutOverrides="GridBox" > 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <GalaSoft_MvvmLight_Command:EventToCommand x:Name="CaptureClick" Command="{Binding CaptureCommand}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button> 
     </Grid>       
    </ScrollViewer> 
</Grid> 

更新:修正了我的問題,所有我需要做的就是確保我的ScrollViewer的高度比它更低真實含有格!

+0

什麼是所有的高度和利潤率?你應該使用具有行/列定義/ StackPanels的網格。我不認爲有任何事情的餘地是個好主意。 – 2012-02-14 19:41:50

+1

可怕的佈局風格! – MyKuLLSKI 2012-02-14 20:01:03

+0

我並沒有真正關注佈局,我所做的所有事情都是在OOTB模板中可用的內容面板網格內的設計表面上引發控件。所有這些格式都是基於我將這些控件放置在另一個之下而生成的代碼。顯然,設計者只是產生可怕的代碼:)。除了添加ScrollViewer之外,我還沒有手動觸摸過xaml。這真是一款沙盒應用程序。 – Cranialsurge 2012-02-15 00:01:33

回答

0

正如別人所說,你應該重新思考你的佈局方式。有更清潔的選擇。但要回答你的問題,有兩個問題。首先是你需要在ScrollViewer上指定一個高度。

<ScrollViewer Height="760"> 
.... 
</ScrollViewer> 

第二個問題是您已應用到網格的241的負邊距。簡單地把它關掉。它阻止了滾動到的範圍內的任何內容。

<Grid x:Name="ContentPanel" Margin="12,0,12,0"> 
.... 
</Grid> 

而且應該這樣做。

+0

謝謝!是的,我肯定需要更多地瞭解佈局。因爲我對基於XAML/Silverlight的UI開發非常陌生,所以我只是在頁面上放置了一些東西。一般來說。 – Cranialsurge 2012-02-15 01:32:34

相關問題