Xamarin Forms 4在CollectionView中使用ItemTemplate绑定时出现的问题

7fyelxc5  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(199)

To customize the appearance of an item in the CollectionView, I'm using a DataTemplateSelector, but unlike what happens in the ListView data binding doesn't seem to work for ItemTemplate in the CollectionView.
I have already tested my solution with a ListView in place of the CollectionView and it works. Everything works fine also if I replace the resource ItemTemplate binding with the inline ItemTemplate.
I'm trying to customize different objects which inherit from ISensor. It looks like

public interface ISensor
{
    string Name { get; set; }
    // ...
}

This is the view model:

public class TestViewModel : MvxNavigationViewModel
{
    private static ObservableCollection<ISensor> _SensorList;

    public TestViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigation)
        : base(logProvider, navigation)
    {
        _SensorList = new ObservableCollection<ISensor> { new Sensor("Thermostat"), null };
    }

    public ObservableCollection<ISensor> SensorsSource { get => _SensorList; }
}

And this is the page containing the CollectionView:

<ContentView>
    <StackLayout>
        <CollectionView ItemsSource="{Binding SensorsSource}" ItemTemplate="{StaticResource SensorsTemplate}" Margin="10">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" Span="2"/>
            </CollectionView.ItemsLayout>

            <CollectionView.EmptyView>
                <StackLayout>
                    <Label Text="Add a new sensor"/>
                </StackLayout>
            </CollectionView.EmptyView>
        </CollectionView>

    </StackLayout>
</ContentView>

The DataTemplateSelector:

public class SensorSelector : DataTemplateSelector
{
    public DataTemplate EmptyTemplate { get; set; }
    public DataTemplate SensorTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if (item is Sensor)
            return SensorTemplate;
        return EmptyTemplate;
    }
}

The DataTemplate present in the App.xaml file:

<DataTemplate x:Key="EmptyDataTemplate">
    <ViewCell>
        <StackLayout BackgroundColor="Yellow">
            <Label Text="EMPTY"/>
        </StackLayout>
    </ViewCell>
</DataTemplate>
<DataTemplate x:Key="SensorDataTemplate">
    <ViewCell>
        <StackLayout BackgroundColor="Red">
            <Label Text="{Binding Name}"/>
        </StackLayout>
    </ViewCell>
</DataTemplate>
<Selectors:SensorSelector x:Key="SensorsTemplate" 
                          EmptyTemplate="{StaticResource EmptyDataTemplate}"
                          SensorTemplate="{StaticResource SensorDataTemplate}"/>

Every time I enter in the TestViewModel the app crash. Is it a binding bug or am I doing something wrong?

ibps3vxo

ibps3vxo1#

使用ListView时,用作ItemTemplate的DataTemplate的子级必须是ViewCell(或从它派生)。使用CollectionView时,不能将ViewCell放在适当的位置。换句话说,不能同时使用ListView和CollectionView的DataTemplate。删除DataTemplate中的ViewCell层应该可以修复在使用CollectionView时出现的崩溃:

<DataTemplate x:Key="EmptyDataTemplate">
    <StackLayout BackgroundColor="Yellow">
        <Label Text="EMPTY"/>
    </StackLayout>
</DataTemplate>

以及SensorDataTemplate的等效更改。

相关问题