C# ListView未显示任何有关可观察集合数据绑定的信息
在这个视图中,我有一个ListView,它应该显示为一个带有可观察字符串集合的数据绑定,但没有显示任何内容 如果我放置了一个标签而不是listview,可观察的集合将其转换为一个简单的字符串,我就会看到数据 在主视图中:C# ListView未显示任何有关可观察集合数据绑定的信息,c#,listview,xamarin.forms,data-binding,observablecollection,C#,Listview,Xamarin.forms,Data Binding,Observablecollection,在这个视图中,我有一个ListView,它应该显示为一个带有可观察字符串集合的数据绑定,但没有显示任何内容 如果我放置了一个标签而不是listview,可观察的集合将其转换为一个简单的字符串,我就会看到数据 在主视图中: <ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding SerialsPorts}"> <ListView.ItemTemplate> <DataTempla
<ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding SerialsPorts}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding SerialPortName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public SerialsPortsViewModel(string serialPortName)
{
SerialPortName = serialPortName;
}
private string serialPortName;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string SerialPortName { get=> serialPortName; set {serialPortName = value ; OnPropertyChanged(nameof(SerialPortName)); } }
我做错了什么?将视图更改为:
<ListView Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding SerialsPorts}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="60">
<StackLayout Orientation="Horizontal">
<BoxView BackgroundColor="Blue" WidthRequest="10" Margin="0,0,0,10" />
<StackLayout BackgroundColor="White" Orientation="Vertical" Margin="5,5,10,5">
<Label Text="{Binding SerialPortName}" FontAttributes="Bold" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
将视图更改为:
<ListView Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding SerialsPorts}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="60">
<StackLayout Orientation="Horizontal">
<BoxView BackgroundColor="Blue" WidthRequest="10" Margin="0,0,0,10" />
<StackLayout BackgroundColor="White" Orientation="Vertical" Margin="5,5,10,5">
<Label Text="{Binding SerialPortName}" FontAttributes="Bold" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
是否设置了BindingContext
?是的,如果我用一个简单的标签替换ListView,并用一个简单的字符串替换observable集合,数据就会出现为什么我认为您要将一组接口绑定到视图?使用断点,我看到observable集合有值,只是在视图中什么也看不见检查模型的名称是SerialsPortsViewModel
或ISerialsPortsViewModel
。是否设置了BindingContext
?是的,如果我用简单的标签替换ListView,用简单的字符串替换可观察的集合,出现数据为什么我认为您正在将一组接口绑定到视图?使用断点,我看到可观察的集合有值,只是在视图中什么都看不见。检查模型的名称是否为SerialsPortsViewModel
或ISerialsPortsViewModel
。