导航时参数无效异常 [英] Argument invalid exception while navigating

查看:28
本文介绍了导航时参数无效异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,当我导航回在 Windows Phone 7 应用程序中首次查看的页面时出现异常

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: 参数不正确.}"

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

我似乎无法找到错误的根源 - 有其他人遇到过这种情况吗?

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

调用堆栈是:

   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 call .Add on一个集合(Pivot 控件有问题)

但我的错误与可观察列表无关,因为我的列表不是从可观察列表类型继承...

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

要导航到的页面的 XAML 也非常简单:

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>

推荐答案

我找到了解决方案.

用户导航是:

  • 加载应用程序并转到 main.xaml(如果导航中没有项目转到 additem.xaml)
  • 输入项目
  • 转到 main.xaml
  • 转到详细信息视图并删除项目
  • 导航回 main.xaml,如果没有项目,我将再次返回

问题是我认为框架中存在错误.我在 main.xaml 中的项目列表不是一个可观察的集合.这意味着在返回 main.xaml 之后,它一定是在尝试从前一个视图加载上下文 - 现在已经更改并且列表中没有项目,因此它崩溃了.

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.

看起来它发生在 2 NavigationContext.GoBack() 上,因为它在不同的线程上触发,并且仅在导航开始发生后才出现 - 很奇怪.

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.

我通过使 main.xaml 上以列表形式显示的集合可观察并使其中的项目遵守 INotifyChanged 来解决此问题.一旦我这样做,一切都神奇地开始工作......

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)

这篇关于导航时参数无效异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆