我有一个奇怪的问题,当我导航回到我的Windows Phone 7应用程序中首次查看的页面时,我得到了一个异常
“{System.ArgumentException:参数不正确。}”
我似乎无法找到错误的来源 - 还有其他人经历过这个吗?
Callstack是:
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)
有趣的是,其他人也有同样的错误:
.NET: ArgumentOutOfRangeException when calling .Add on a collection (Trouble with Pivot control)
但我的错误与可观察列表无关,因为我的列表不从可观察列表类型继承...
导航到的页面的XAML也非常简单:
<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>
答案 0 :(得分:2)
我找到了解决方案。
用户导航是:
问题是我认为框架中存在错误。我在main.xaml中的项目列表不是一个可观察的集合。这意味着在返回main.xaml之后,它一定是在尝试从上一个视图加载上下文 - 现在已经改变了,列表中没有项目,所以它崩溃了。
它似乎发生在2 NavigationContext.GoBack()上,因为它在另一个线程上触发,只在导航开始发生后出现 - 很奇怪。
我解决了这个问题,方法是在main.xaml上以列表形式显示的集合可观察并使内部的项目遵循INotifyChanged。一旦我这样做,一切都神奇地开始工作......
感谢所有试图提供帮助的人 - 您的意见非常感谢(我的赞成表示感谢)答案 1 :(得分:1)
您是否有任何可能导致以下情况的代码:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if(//some condition where page should go back )
{
if(NavigationService.CanGoBack)
NavigationService.GoBack();
}
}
答案 2 :(得分:1)
如果您在同一页面上导航,可能会发生这种情况。 (如果RootFrame.OriginalSource是“Sample.xaml”而你正在做Navigate(“Samle.xaml”,UriKind.Relative))
答案 3 :(得分:1)
好的,基于堆栈,看起来你正在使VirtualizingStackPanel访问超出界限的列表。可能是框架中的错误,但更有可能的是你正在做一些它没有预期的事情。您是否手动(以编程方式)向面板添加和删除内容?当你检测到滚动时,你在做什么吗?看看你是如何在该页面上操纵StackPanel的。
此外,根据这些新信息以及异常消息的内容,他们建议您进行双导航似乎是错误的。我原本认为当我看到你的标题时就是问题,所以我明白它们来自哪里,但在这种情况下的例外是“片段不受支持”。这显然不是这种情况。