Argument invalid exception while navigating

2020-06-29 04:26发布

问题:

I have a weird issue, when I navigate back to the page first viewed in my windows phone 7 application i get an exception

"{System.ArgumentException: The parameter is incorrect. }"

I cannot seem to find the source of the error - has anyone else experienced this?

Callstack is:

   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
   at System.Windows.Controls.ItemCollection.GetItemImpl(Int32 index)
   at System.Windows.Controls.ItemCollection.GetItemImplSkipMethodPack(Int32 index)
   at System.Windows.PresentationFrameworkCollection`1.get_Item(Int32 index)
   at System.Windows.Controls.VirtualizingStackPanel.CleanupContainers(ItemsControl itemsControl)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
   at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.MeasureNative(IntPtr element, Single inWidth, Single inHeight)
   at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
   at Microsoft.Phone.Controls.PhoneApplicationFrame.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)

Interestingly enough someone else has the same error:

.NET: ArgumentOutOfRangeException when calling .Add on a collection (Trouble with Pivot control)

but my error is not related to a watchable list, as my list is not inherriting from the observable list types...

the XAML for the page being navigated to is really simple as well:

<phone:PhoneApplicationPage 
    x:Class="TestApp.PhoneApplication.AddItem"
    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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--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="TestApplication" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Height="72" HorizontalAlignment="Left" Margin="0,324,0,0" Name="txtItemName" Text="Item name" VerticalAlignment="Top" Width="450" GotFocus="txtItemName_GotFocus" LostFocus="txtItemName_LostFocus" />
            <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="0,402,0,0" Name="btnSave" VerticalAlignment="Top" Width="160" Click="btnSave_Click" />
            <Button Content="Cancel" Height="72" HorizontalAlignment="Left" Margin="290,402,0,0" Name="btnCancel" VerticalAlignment="Top" Width="160" Click="btnCancel_Click" />
        </Grid>
        <ProgressBar Height="4" Style="{StaticResource PerformanceProgressBar}"  HorizontalAlignment="Left" Margin="12,480,0,0" Name="progressBar1" VerticalAlignment="Top" Width="460" Grid.Row="1" Opacity="0" Background="#FFCF2B26" Foreground="#FFCF2B26" />
    </Grid>
</phone:PhoneApplicationPage>

回答1:

I found the solution to this.

a users navigation was:

  • load application and go to main.xaml (if no items go to additem.xaml in onnavigating)
  • enter item
  • go forward to main.xaml
  • go to detail view and delete item
  • navigate back to main.xaml where if there are not items i goback AGAIN

the problem was i think a bug in the framework. my list of items in main.xaml was not an observable collection. this means that after going back to the main.xaml it must have been trying to load the context from the previous view - which had now changed and there were no items in the list, so it crashed.

It looked like it was happening on the 2 NavigationContext.GoBack() because it was firing on a different thread and only appearing AFTER the navigation had started to occur - weird.

i resolved this issue by making the collection that was on display in list form on main.xaml observable and making the items inside adhere to INotifyChanged. Once i did this everything magically started working...

thanks to everyone who tried to help - your input was really appreciated (my upvotes show this gratitude)



回答2:

Do you have any code which would lead to the situation like:

protected override void  OnNavigatedTo(NavigationEventArgs e)
    {
        if(//some condition where page should go back )
        {
            if(NavigationService.CanGoBack)
                NavigationService.GoBack();
        }
    }


回答3:

It could happen, if you navigating on the same page, where you are. (If RootFrame.OriginalSource is "Sample.xaml" and you are doing Navigate("Samle.xaml", UriKind.Relative))



回答4:

Okay, based on the stack it looks like you're causing the VirtualizingStackPanel to access a list out of bounds. Could be a bug in the framework, but more likely you're doing something that it isn't expecting. Are you manually (programmatically) adding and removing things to the panel? Are you doing things when you detect scrolling? Look at how you're manipulating the StackPanel on that page.

Also, they people suggesting that you're double-navigating seem to be wrong based on this new information as well as the content of the exception message. I originally thought that was the problem when I saw your title, so I understand where they're coming from, but the exception in that case is something like "Fragments are not supported". Which clearly isn't the case here.