C# GridView.ItemsSource在创建新窗口时中断
当我在导航到新页面时运行代码时:C# GridView.ItemsSource在创建新窗口时中断,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,当我在导航到新页面时运行代码时: this.Frame.Navigate(typeof(BlankPage1), pages); 当连接到BlankPage1.xaml.cs中的以下代码时,该代码运行良好并生成正确的输出 protected override void OnNavigatedTo(NavigationEventArgs e) { _thumbnails = e.Parameter as ObservableCollection<ThumbnailImage
this.Frame.Navigate(typeof(BlankPage1), pages);
当连接到BlankPage1.xaml.cs中的以下代码时,该代码运行良好并生成正确的输出
protected override void OnNavigatedTo(NavigationEventArgs e)
{
_thumbnails = e.Parameter as ObservableCollection<ThumbnailImage>;
FileView.ItemsSource = _thumbnails;
}
要创建新视图并导航到同一xaml文件,它会抛出一个异常:“exception Trowned:'System.exception'in Viewer.exe”
但是,当OnNavigatedTo更改为
protected override void OnNavigatedTo(NavigationEventArgs e)
{
_thumbnails = e.Parameter as ObservableCollection<ThumbnailImage>;
//FileView.ItemsSource = _thumbnails;
}
而xaml是
<Page Width="300" Height="850"
x:Class="Viewer.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Viewer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<GridView x:Name="FileView" Width="256" Height="850" Margin="0,20,0,0">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:ThumbnailImage">
<Grid>
<Button FontFamily="Segoe MDL2 Assets" Content="" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Image CanDrag="True" Stretch="Uniform" Source="{x:Bind Source}" Height="100" Margin="10,40"/>
<Border Opacity=".8" Background="Black" VerticalAlignment="Bottom"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" ScrollViewer.VerticalScrollBarVisibility="Visible"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</ScrollViewer>
</Page>
有两个潜在问题:
frame.Navigate
,直到加载应用程序。在代码示例中,您正在尝试在将帧分配给Window.Current.Content
之前进行导航。您可以尝试将其作为lambda块中的最后一行移动。您甚至可能需要同步运行导航,或者至少在UI线程上运行导航RunAsync
可能会导致问题。您可以使用Dispatcher.Invoke
进行测试,看看它是否仍然存在相同的问题不建议在UWP中的两个不同视图之间传递
可观察集合
(或任何其他INotifyPropertyChanged
-或INotifyCollectionChanged
-基于模型)
原因是UWP中的每个视图都有自己的UI线程,这也是您需要使用新创建视图的Dispatcher
导航到页面的原因。但是,当您使用数据绑定时,对集合的任何更改都将执行CollectionChanged
,这将对两个应用程序视图运行。这将不可避免地使应用程序崩溃,因为CollectionChanged
和PropertyChanged
会影响UI,因此必须在特定视图的UI线程上运行-在这种情况下,每个视图都有自己的UI线程
因此,为了确保避免这些问题,请创建一个新集合,以确保每个视图都有自己的observedcollection
实例:
var secondaryViewPages = new ObservableCollection<ThumbnailImage>(pages);
frame.Navigate(typeof(BlankPage1), secondaryViewPages);
var secondaryViewPages=新的ObservableCollection(页);
框架导航(typeof(BlankPage1),secondaryViewPages);
注意:如果实现了ThumbnailImage
INotifyPropertyChanged
,您还必须创建集合中每个图像的“副本”。但是,在这种情况下,这是不必要的,因为ThumbnailImage
是一个POCO,它不会通知用户界面的更改
另请注意:不建议使用集合作为导航
的参数,因为它会阻止您在应用程序挂起期间序列化应用程序状态,请参阅:
当应用程序挂起时,应用程序通常使用GetNavigationState序列化帧的状态。您可以在应用程序代码中直接执行此操作,也可以使用VisualStudio模板生成的SuspendionManager类间接执行此操作。要使用GetNavigationState启用帧状态序列化,必须仅为导航参数使用基本类型,如字符串、字符、数字和GUID类型。否则,GetNavigationState将在应用程序挂起时引发异常。如果不使用GetNavigationState,则该参数可以具有其他类型
相反,您可以以不同的方式在视图之间共享数据,就像让两个视图都可以访问单例服务一样。谢谢,我可以更改代码,这样我就可以传递一个对象,而不是一个ObservableCollection,它工作了。太棒了,很高兴它有帮助:-)
<Page Width="300" Height="850"
x:Class="Viewer.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Viewer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<GridView x:Name="FileView" Width="256" Height="850" Margin="0,20,0,0">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:ThumbnailImage">
<Grid>
<Button FontFamily="Segoe MDL2 Assets" Content="" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Image CanDrag="True" Stretch="Uniform" Source="{x:Bind Source}" Height="100" Margin="10,40"/>
<Border Opacity=".8" Background="Black" VerticalAlignment="Bottom"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" ScrollViewer.VerticalScrollBarVisibility="Visible"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</ScrollViewer>
</Page>
var secondaryViewPages = new ObservableCollection<ThumbnailImage>(pages);
frame.Navigate(typeof(BlankPage1), secondaryViewPages);