UWP - 向ListView添加项目会动态导致崩溃

时间:2015-11-17 16:56:54

标签: xaml listview windows-10 uwp

我有一个像这样定义的列表视图:

<ListView 
    x:Name="phonesListView" 
    Grid.Row="5"
    Background="Black"
    IsItemClickEnabled="True"
    Foreground="Gray"  >

    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="auto" HorizontalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Grid 
                        Grid.Row="0">
                    <Grid.ColumnDefinitions >
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <ComboBox 
                            Grid.Column="0"
                            VerticalAlignment="Center"
                            Background="Black"
                            Foreground="White">
                        <ComboBoxItem Content="Home" IsSelected="True"/>
                        <ComboBoxItem Content="{Binding Type}"/>
                        <ComboBoxItem Content="Office"/>
                        <ComboBoxItem Content="Fax"/>
                    </ComboBox>

                    <Button
                            Grid.Column="1"
                            Height="30"
                            Width="30"
                            Foreground="Black"
                            Margin="0, 5, 0, 5"
                            HorizontalAlignment="Center" Click="RemovePhone">
                        <Button.Background>
                            <ImageBrush Stretch="Fill" ImageSource="Assets/appbar.close.png" />
                        </Button.Background>
                    </Button>

                </Grid>

                <TextBox 
                    Grid.Row="1"
                    Background="White"
                    Foreground="Black"
                    FontSize="20"
                    InputScope="TelephoneNumber"
                    Text="{Binding Number}"/>

            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我有private ObservableCollection<Phone> numbers = new ObservableCollection<Phone>();

在构造函数中,我调用phonesListView.ItemSource = numbers;

在某些按钮上单击我想将新项目添加到listView,所以我调用了一个方法:

private void AddPhone(object sender, RoutedEventArgs e) {
    Phone phone = new Phone("", Types.HOME);
    numbers.Add(phone);
}

但是按下按钮后点击添加项目,应用程序崩溃并调用App.g.i.cs并突出显示global::System.Diagnostics.Debugger.Break();

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
#endif

This is e parameter

我是Universal Windows App的新手,我读到当XAML代码出现问题时会调用它。你能帮帮我吗?感谢。

1 个答案:

答案 0 :(得分:0)

我尝试使用ItemsSource="{x:Bind Mode=TwoWay}"

我可以动态更新listview的唯一方法(但不是最好的)我正在制作一个处理按钮事件的方法,我在其中添加一个新元素(调用addmethod)并更新列表的ItemsSource属性图。

private  void AnadeDoctorAppBarButton_Click(object sender, RoutedEventArgs e)
{

              Doctor A = new Doctor("Mapache", "dentista", "La calle", "La vida", "Lo que sea", "Direccion", "olos");

    ViewModel.AnadeDoctor = A;
    ListaDePrueba.ItemsSource = ViewModel.ColeccionDoctores;

}

它有效,但我认为这不是真正的数据绑定。