none
使用GridView崩溃问题 RRS feed

  • 问题

  • 我在一个blank页面里创建一个GridView,然后通过ObservableCollection绑定,当删除最后一个数据的时候,XAML解析总崩溃!!!

    代码如下

    XAML:

    <Page
        x:Class="BlankApp.MainPage"
        IsTabStop="false"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:BlankApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Page.Resources>
            <DataTemplate x:Key="ItemTemplate">
                <Grid>
                    <Rectangle Height="100" Width="300" Fill="Red" PointerPressed="Rectangle_PointerPressed_1"/>
                </Grid>
            </DataTemplate>
    
            <Style x:Key="ItemStyle"  TargetType="GridViewItem">
                <Setter Property="Padding" Value="0" />
                <Setter Property="Margin" Value="0,0,20,20" />
                <Setter Property="TabNavigation" Value="Local" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Stretch" />
                <Setter Property="Background" Value="White"/>
                <Setter Property="Width" Value="300"/>
            </Style>
    
            <ItemsPanelTemplate x:Key="ItemPanelTemplate">
                <WrapGrid Orientation="Vertical" Margin="108 36 32 12"/>
            </ItemsPanelTemplate>
    
        </Page.Resources>
    
        <GridView x:Name="mLayout"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch"
                        ItemContainerStyle="{StaticResource ItemStyle}"
                        ItemsPanel="{StaticResource ItemPanelTemplate}"
                        ItemTemplate="{StaticResource ItemTemplate}"
                        IsSwipeEnabled="False"
                        SelectionMode="None"
                        ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                 >
        </GridView>
    </Page>
    

    CS:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace BlankApp
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
            }
    
            public ObservableCollection<string> mFavList = new ObservableCollection<string>();
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                mLayout.ItemsSource = mFavList;
                mFavList.Add("111");
            }
    
            private void Rectangle_PointerPressed_1(object sender, PointerRoutedEventArgs e)
            {
                mFavList.Remove((sender as FrameworkElement).DataContext as string);
                //mFavList.RemoveAt(0);
            }
        }
    }
    

    2012年8月6日 8:34

答案