0

I have program app by winRT(Win phone 8.1) and i use FlipView control as below:

 <FlipView x:Name="flvMain" Margin="0,0,0,0" Background="Transparent" SelectedIndex="1" SelectionChanged="flvMain_SelectionChanged" Tapped="flvMain_Tap">
         <!-- Flip View Item 1 -->
        <Grid x:Name="grdOption" >
            <Grid.RowDefinitions>
                <RowDefinition Height="120"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.Background>
                    <ImageBrush Stretch="Fill" ImageSource="Assets/test1.png"/>
                </Grid.Background>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <Image x:Name="imgAvatar" Grid.Column="0" Margin="10,15" Source="Assets/Logo.png"/>
                <Grid Grid.Column="1" Margin="0,15">
                    <TextBlock x:Name="txbNameAlias" Margin="0,5,0,54" FontSize="22" FontWeight="Bold" SelectionHighlightColor="{x:Null}" Foreground="White" RequestedTheme="Light">Name</TextBlock>
                    <TextBlock x:Name="txbAccountId" Margin="0,36,0,0" FontSize="16" RequestedTheme="Light" Foreground="White" Height="20" VerticalAlignment="Top">55556666</TextBlock>
                    <TextBlock x:Name="txbAmount" Margin="0,61,0,0" FontSize="18" FontWeight="Bold" RequestedTheme="Light" Foreground="#FFFFFEFE">555555</TextBlock>
                </Grid>
                <Button x:Name="btnBackToMain" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="50" MinWidth="40" Margin="5,30" BorderBrush="Transparent" RenderTransformOrigin="0.54,-0.035" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Click="btnBackToMain_Click">
                    <Button.Background>
                        <ImageBrush Stretch="Fill" ImageSource="Assets/icon-back-mainpage.png"/>
                    </Button.Background>
                </Button>
            </Grid>
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" BorderThickness="0" RequestedTheme="Light" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF0C75C9" Margin="0,-10,0,-10" MinHeight="58" Content="Btn 1" ScrollViewer.VerticalScrollBarVisibility="Disabled" Foreground="#FFFFFEFE"  FontSize="18" FontFamily="Global User Interface" />
                <Button Grid.Column="1" BorderThickness="0" RequestedTheme="Light" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF308D1E" Margin="0,-10" MinHeight="58" ScrollViewer.VerticalScrollBarVisibility="Disabled" FontFamily="Global User Interface" FontSize="18" Content="btn 2" Foreground="White"/>
            </Grid>
            <Grid Grid.Row="2">
                <ListView x:Name="lvFunctionOption" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemClick="lvFunction_ItemClick" IsItemClickEnabled="True">
                   <!-- some code here -->
                </ListView>
                <Grid x:Name="grdSubListFunction" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <ListView Grid.Column="1" x:Name="lvSubFunctions" Background="#00D53636" Margin="0" Visibility="Collapsed" RequestedTheme="Light" IsItemClickEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Top" ItemClick="lvSubFunctions_ItemClick">
                       <!-- some code here -->
                    </ListView>
                </Grid>
            </Grid>
        </Grid>
         <!-- Flip View Item 2 -->
        <Grid x:Name="grdMain" Height="640" Margin="0,0,0,0" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Background="#FF0C75C9">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <Image x:Name="btnToListFunction" Grid.Column="0" Source="Assets/icon-menu.png" Margin="7" Tapped="btnToListFunction_Tap"></Image>

            </Grid>
            <ScrollViewer Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                <StackPanel Background="White">
                    <!-- some code here -->
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </FlipView>

and in xaml.cs file i have code

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        flvMain.SelectedIndex = 1;
    }

it run normal but when i click on btnBackToMain and tap on btnToListFunction i have code:

 private void btnBackToMain_Click(object sender, RoutedEventArgs e)
    {
        flvMain.SelectedIndex = 1;
    }

    private void btnToListFunction_Tap(object sender, TappedRoutedEventArgs e)
    {
        flvMain.SelectedIndex = 0;
    }

flipview can't change item to sepecific index. When I debug click event by SelectionChangedEvent

    private void flvMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (flvMain != null)
        {
            CustomTool.Log(this, "current index: " + flvMain.SelectedIndex);
        }
        else
        {
            CustomTool.Log(this, "NULLL");
        }
    }

I have result as with tap btnToListFunction :

Log: MainPage >> current index: 0
Log: MainPage >> current index: 1

and click btnBackToMain

Log: MainPage >> current index: 1
Log: MainPage >> current index: 0

But when I swipe to right(same click on btnBackToMain) i have result:

Log: MainPage >> current index: 1

and swipe to left (same click on btnToListFunction ) i have result:

Log: MainPage >> current index: 0

Please help me solve it? I don't know why selectedIndex of flipview can be roll back after it change (by click). Thanks for all suport!

4

1 回答 1

1

我有类似的问题,不同之处在于我的按钮是AppBarButton。如果您使用鼠标单击按钮,它会正常工作;因此,我猜这个问题是由操纵发起的一些事件引起的。我通过声明来解决这个问题

ManipulationMode="None"

对于 FlipView 内的按钮。

ManipulationMode="None"因此,您可以通过嵌入 XAML 中的btnBackToMainbtnToListFunction元素来解决此问题。

希望此解决方案对您有所帮助。

[2015-08-18] 新增

如果您不想将 Manipulation 设置为 None 以忽略对按钮的所有操作,您可以尝试在将flvMain.SelectedIndex = -1;设置为所需数字之前进行设置flvMain.SelectedIndex。例如,

      private void btnBackToMain_Click(object sender, RoutedEventArgs e)
    {
        flvMain.SelectedIndex = -1;
        flvMain.SelectedIndex = 1;
    }

    private void btnToListFunction_Tap(object sender, TappedRoutedEventArgs e)
    {
        flvMain.SelectedIndex = -1;
        flvMain.SelectedIndex = 0;
    }        

因为问题仅在原始索引成功下一个索引并且 -1 可用于 SelectedIndex 时才会出现,因为没有选择任何内容。

为什么 FlipView 会这样?我不知道。或许它可以避免人们触碰它时的不稳定行为。^_^

于 2015-07-28T09:12:09.303 回答