-->

WindowPhone FlipView System.ArgumentException

2019-05-30 08:19发布

问题:

I have a problem with using FlipView in my WindowsPhone app. I have a Page with FlipView, which has ItemsSource that binds to ItemsGroup and SelectedItem that binds to CurrentItem. DataTemplate of FlipView includes WebView which has attached property Html that binds to Html of CurrentItem. Everything goes well, but the app crashes with System.ArgumentException from time to time and I have no idea what's wrong.

XAML:

<Page
x:Class="UkraineNews.ItemPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:common="using:News.Common"
xmlns:viewModel="using:News.ViewModel"
xmlns:converter="using:News.Converter">
<Page.DataContext>
    <viewModel:PhoneGroupViewModel/>
</Page.DataContext>
<Page.Resources>
    <converter:DateConverter x:Key="DateConverter" 
                             IsShowFullDate="True"/>
</Page.Resources>
<Grid>
    <FlipView ItemsSource="{Binding Group.Items}"
              SelectedItem="{Binding Group.CurrentItem, Mode=TwoWay}">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Border Grid.Row="0" 
                            Background="{StaticResource ItemBorderBackground}"
                            HorizontalAlignment="Right"
                            Padding="5"
                            CornerRadius="0,0,0,5">
                        <TextBlock Text="{Binding Published, Converter={StaticResource DateConverter}}"/>
                    </Border>
                    <TextBlock Grid.Row="1"
                               TextWrapping="Wrap"
                               Text="{Binding Title}"
                               FontWeight="Bold"
                               Margin="24,0"
                               FontSize="20"
                               Foreground="{StaticResource LeftMenuBackgroundBrush}"/>
                    <WebView Grid.Row="2" 
                             NavigationStarting="WebView_NavigationStarting"
                             common:WebViewBehaviour.Html="{Binding Html}"/>
                </Grid>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>
</Grid>

C#:

  public class NewsItem 
  {
     public string Title { get; set; }
     public string Link { get; set; }
     public DateTime Published { get; set; }
     public string Html { get; set; }
     public string Image { get; set; }
  }

Error:

The parameter is incorrect. System.ArgumentException: Value does not fall within the expected range.

回答1:

I've replaced default Templete for FlipView with my custom one, in which i remove 2.5 point margin of Border. I don't know how, but it works. Maybe it connected with the pointer that get onto margin area, because error took place when swipe from the edge.

<Style x:Key="FlipViewStyle" TargetType="FlipView">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="TabNavigation" Value="Once"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="False"/>
    <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="False"/>
    <Setter Property="ScrollViewer.IsHorizontalScrollChainingEnabled" Value="True"/>
    <Setter Property="ScrollViewer.IsVerticalScrollChainingEnabled" Value="True"/>
    <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/>
    <Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel AreScrollSnapPointsRegular="True" 
                                        Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="FlipView">
                <Grid>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}">
                        <Grid>
                            <ScrollViewer x:Name="ScrollingHost" 
                                          AutomationProperties.AccessibilityView="Raw" 
                                          BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}" 
                                          HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" 
                                          HorizontalSnapPointsType="MandatorySingle"
                                          HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" 
                                          IsTabStop="False" 
                                          IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" 
                                          IsHorizontalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsHorizontalScrollChainingEnabled}" 
                                          IsVerticalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsVerticalScrollChainingEnabled}" 
                                          IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" 
                                          IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" 
                                          Padding="{TemplateBinding Padding}" 
                                          TabNavigation="{TemplateBinding TabNavigation}" 
                                          VerticalSnapPointsType="MandatorySingle" 
                                          VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" 
                                          VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" 
                                          ZoomMode="Disabled">
                                <ItemsPresenter/>
                            </ScrollViewer>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>