locked
WP8.1 FlipView索引恢复0的问题 RRS feed

  • 问题

  • 一个页面中只有一个FlipView,内嵌一个

    <FlipView.ItemTemplate>
                    <DataTemplate>
                        <Grid Loaded="img_loaded">
                            <Image HorizontalAlignment="Center" ImageFailed="LoadImgFaile" ImageOpened="LoadImgSucees" Source="{Binding ImagePath}" >
                            </Image>
                        </Grid>
                    </DataTemplate>
                </FlipView.ItemTemplate>

    打开后在当前页面跳转到其他页面,再返回的时候数据什么的都正常,但是只要稍微一划动FlipView的索引就会回到0。比如我打开的图片所在的项索引是5,返回后无论是向左向右一划动索引就会变成0,显示所因为0的项。

    这个是什么问题?

    贴个代码:

    <Page
        x:Class="test.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:test"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid Name="LayoutRoot" 
               Loaded="hide" 
               Margin="0,-26.667,0,-60.333">
    
            <FlipView HorizontalAlignment="Stretch"
                      Loaded="flipview"
                      ItemsSource="{Binding imItems}"
                      Name="FlipPicture"
                      
                      PointerPressed="PointerPressed_eventdone" 
                      PointerExited="PointerExited_eventdone" 
                      SelectionChanged="changNumber">
                <FlipView.ItemTemplate>
                    <DataTemplate>
                        <ScrollViewer 
                                      ZoomMode="Enabled"
                                      MinZoomFactor="1"
                                      MaxZoomFactor="5"
                                      HorizontalScrollBarVisibility="Hidden" 
                                      VerticalScrollBarVisibility="Hidden" 
                                      HorizontalScrollMode="Auto"
                                      VerticalScrollMode="Auto"
                                      >
    
                            <Grid Width="490">
                                <Grid.ChildrenTransitions>
                                    <TransitionCollection>
                                        <ContentThemeTransition/>
                                    </TransitionCollection>
                                </Grid.ChildrenTransitions>
                                <Image 
                                   Stretch="Uniform"
                                   
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   
                                   Name="image" 
                                   Source="{Binding ImagePath}" >
                                </Image>
                            </Grid>
    
                        </ScrollViewer>
                    </DataTemplate>
                </FlipView.ItemTemplate>
            </FlipView>
    
            <TextBlock Name="Number"
                       TextWrapping="Wrap" Text="" 
                       HorizontalAlignment="Center"
                       VerticalAlignment="Bottom" Margin="0,0,0,60"/>
        </Grid>
    
        <Page.BottomAppBar>
            <CommandBar x:Name="Bar" Opacity="0.5">
                <AppBarButton x:Uid="SaveAppBarButton"    
                              x:Name="SaveAppBarButton" 
                              Label="保存" Icon="Save"      
                             />
                <CommandBar.SecondaryCommands>
                    <AppBarToggleButton x:Uid="Search" 
                                        Label="vv" 
                                        Click="vv_click"/>
                </CommandBar.SecondaryCommands>
            </CommandBar>
    
        </Page.BottomAppBar>
    </Page>


    namespace test
    {
        /// <summary>
        /// 可用于自身或导航至 Frame 内部的空白页。
        /// </summary>
        public sealed partial class MainPage : Page
        {
    
            bool PointerPress = false;
            public List<DataItem> imItems { get; set; }
            public MainPage()
            {
                this.InitializeComponent();
    
                this.NavigationCacheMode = NavigationCacheMode.Required;
    
                imItems = new List<DataItem>();
    
                for (int i = 0; i<10; i++)
                    imItems.Add(new DataItem { ImagePath = "Assets/SmallLogo.scale-240.png" });
            }
    
            /// <summary>
            /// 在此页将要在 Frame 中显示时进行调用。
            /// </summary>
            /// <param name="e">描述如何访问此页的事件数据。
            /// 此参数通常用于配置页。</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                // TODO: 准备此处显示的页面。
    
                // TODO: 如果您的应用程序包含多个页面,请确保
                // 通过注册以下事件来处理硬件“后退”按钮:
                // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
                // 如果使用由某些模板提供的 NavigationHelper,
                // 则系统会为您处理该事件。
            }
    
            private void vv_click(object sender, RoutedEventArgs e)
            {
                Frame.Navigate(typeof(BasicPage1));
            }
    
            private void PointerPressed_eventdone(object sender, PointerRoutedEventArgs e)
            {
                if (PointerPress == false)
                    PointerPress = true;
                else
                    PointerPress = false;
            }
    
            private void PointerExited_eventdone(object sender, PointerRoutedEventArgs e)
            {
                if (PointerPress == true)
                {
                    Bar.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    Bar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
                }
    
                else
                {
                    //                Bar.ClosedDisplayMode = AppBarClosedDisplayMode.Minimal;
                    Bar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    
                }
            }
    
            private void changNumber(object sender, SelectionChangedEventArgs e)
            {
                if(FlipPicture != null)
                {
                    string str = string.Format("{0}/{1}", FlipPicture.SelectedIndex + 1, imItems.Count);
                    Number.Text = str;
                }
            }
    
            private void hide(object sender, RoutedEventArgs e)
            {
                Bar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
    
            private void flipview(object sender, RoutedEventArgs e)
            {
                var f = sender as FlipView;
                f.ItemsSource = imItems;
            }
        }
    
        public class DataItem
        {
            public string ImagePath { get; set; }
        }
    }

    执行vv_click后再按返回,滑动flipview索引会返回0(导航的页面是随便的一个页面)

    • 已编辑 zh-hai 2015年4月4日 5:07 更新代码
    2015年3月15日 4:23

全部回复