对象引用未设置为...在 COLLECTIONVIEW 中的命令中
Object reference not set to... IN COMMAND IN COLLECTIONVIEW
我有一个 collectionview,command.The 问题是当我将对象读取到命令时,由于某种原因它为零,抱歉,请帮助解决错误
XAML
<CollectionView ItemsSource="{Binding MyPins}" x:Name="ListPlaces"
SelectionMode="None">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="10"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame HasShadow="True" HorizontalOptions="FillAndExpand" BorderColor="#E5E5E5" CornerRadius="10" VerticalOptions="Start">
<StackLayout Orientation="Vertical">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.CallPlace, Source={x:Reference ListPlaces}}" ></TapGestureRecognizer>
</StackLayout.GestureRecognizers>
<Label x:Name="NameOfPlace" Text="{Binding Name}" TextColor="#2D78FD" FontSize="14" FontFamily="Robobo"/>
<Label x:Name="AdressOfPlace" Text="{Binding Address}" TextColor="#616161" FontSize="12" FontFamily="Robobo"/>
<StackLayout Orientation="Horizontal">
<Label x:Name="TimeWork" VerticalOptions="Center" Text="Open until - " TextColor="#616161" FontSize="12" FontFamily="Robobo"/>
<Label x:Name="TimeWork1" VerticalOptions="Center" Text="{Binding Opened}" TextColor="#616161" FontSize="12" FontFamily="Robobo"/>
<Image Source="openIcon" VerticalOptions="Center" />
<Button HeightRequest="24" VerticalOptions="Center" Padding="0,-3,0,0" WidthRequest="92" HorizontalOptions="EndAndExpand" FontSize="14" CornerRadius="45" BackgroundColor="#2D78FD" TextColor="White" Text="Call up"/>
</StackLayout>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
代码隐藏
public PlacesList()
{
InitializeComponent();
BindingContext = this;
CallPlace = new Command<CustomPin>((key) =>
{
var currentPlace = key as CustomPin;
MapPage.PlacesHandler(currentPlace);
});
}
使用命令参数将数据传递到命令中属性
<TapGestureRecognizer CommandParameter={Binding .} Command="{Binding BindingContext.CallPlace, Source={x:Reference ListPlaces}}" />
假设 CustomPin 是您要绑定到此 CollectionView 的列表对象的类型。
此外,使用免费的 XAML 样式器和扩展,它使您的 XAML 非常漂亮
每次保存文件
https://marketplace.visualstudio.com/items?itemName=TeamXavalon.XAMLStyler
我有一个 collectionview,command.The 问题是当我将对象读取到命令时,由于某种原因它为零,抱歉,请帮助解决错误
XAML
<CollectionView ItemsSource="{Binding MyPins}" x:Name="ListPlaces"
SelectionMode="None">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="10"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame HasShadow="True" HorizontalOptions="FillAndExpand" BorderColor="#E5E5E5" CornerRadius="10" VerticalOptions="Start">
<StackLayout Orientation="Vertical">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.CallPlace, Source={x:Reference ListPlaces}}" ></TapGestureRecognizer>
</StackLayout.GestureRecognizers>
<Label x:Name="NameOfPlace" Text="{Binding Name}" TextColor="#2D78FD" FontSize="14" FontFamily="Robobo"/>
<Label x:Name="AdressOfPlace" Text="{Binding Address}" TextColor="#616161" FontSize="12" FontFamily="Robobo"/>
<StackLayout Orientation="Horizontal">
<Label x:Name="TimeWork" VerticalOptions="Center" Text="Open until - " TextColor="#616161" FontSize="12" FontFamily="Robobo"/>
<Label x:Name="TimeWork1" VerticalOptions="Center" Text="{Binding Opened}" TextColor="#616161" FontSize="12" FontFamily="Robobo"/>
<Image Source="openIcon" VerticalOptions="Center" />
<Button HeightRequest="24" VerticalOptions="Center" Padding="0,-3,0,0" WidthRequest="92" HorizontalOptions="EndAndExpand" FontSize="14" CornerRadius="45" BackgroundColor="#2D78FD" TextColor="White" Text="Call up"/>
</StackLayout>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
代码隐藏
public PlacesList()
{
InitializeComponent();
BindingContext = this;
CallPlace = new Command<CustomPin>((key) =>
{
var currentPlace = key as CustomPin;
MapPage.PlacesHandler(currentPlace);
});
}
使用命令参数将数据传递到命令中属性
<TapGestureRecognizer CommandParameter={Binding .} Command="{Binding BindingContext.CallPlace, Source={x:Reference ListPlaces}}" />
假设 CustomPin 是您要绑定到此 CollectionView 的列表对象的类型。
此外,使用免费的 XAML 样式器和扩展,它使您的 XAML 非常漂亮 每次保存文件
https://marketplace.visualstudio.com/items?itemName=TeamXavalon.XAMLStyler