Xamarin.forms CollectionView SelectedItem BindableProperty

Xamarin.forms CollectionView SelectedItem BindableProperty,xamarin.forms,Xamarin.forms,我有收藏视图,我想进入详细信息页面。然而,我使用的是BindableObject类Xamarin表单,它工作得很好 所以创建属性,然后像这样使用它 public BindableProperty DiagnosticEventProperty = BindableProperty.Create( propertyName: nameof(Logs), returnType: typeof(ObservableCollection<JsonLogEvent&g

我有收藏视图,我想进入详细信息页面。然而,我使用的是BindableObject类Xamarin表单,它工作得很好

所以创建属性,然后像这样使用它

public BindableProperty DiagnosticEventProperty = BindableProperty.Create(
        propertyName: nameof(Logs),
        returnType: typeof(ObservableCollection<JsonLogEvent>),
        defaultValue: new ObservableCollection<JsonLogEvent>(),
        declaringType: typeof(DiagnosticInfoViewModel));
    
    public ObservableCollection<JsonLogEvent> Logs
    {
        get => (ObservableCollection<JsonLogEvent>) GetValue(DiagnosticEventProperty);
        set => SetValue(DiagnosticEventProperty, value);
    }
public BindableProperty DiagnosticEventProperty=BindableProperty.Create(
propertyName:nameof(日志),
returnType:typeof(ObservableCollection),
defaultValue:new ObservableCollection(),
declaringType:typeof(诊断信息视图模型));
公共可观测收集日志
{
get=>(ObservableCollection)GetValue(DiagnosticEventProperty);
set=>SetValue(DiagnosticEventProperty,值);
}
我是否不确定如何处理所选项目

我的Xaml

<CollectionView HorizontalOptions="CenterAndExpand" SelectionMode="Multiple"
                        VerticalOptions="FillAndExpand"
                        VerticalScrollBarVisibility="Never"
                        SelectedItem="{Binding SelectedLogs }"
                        ItemsSource="{Binding Logs, Mode=TwoWay}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <templates:DiagnosticEventPageTemplate />
                </DataTemplate>
            </CollectionView.ItemTemplate>
    </CollectionView>

ItemsCoure指定要显示的项目集合,除了为ItemsCoure属性设置绑定外,我们还需要使用模型类(应该是JsonLogEvent类)为模板设置绑定

例如,“JsonLogEvent”类包含“TheContent”参数,我们使用该参数为标签设置绑定

检查代码:

<CollectionView ItemsSource="{Binding DataCollection}" >
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout>
                <Label Text="{Binding TheContent}"/>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

根据发布的代码,您似乎使用了自定义布局。要在collectionView中使用自定义布局,请设置自定义控件的绑定,如:

<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestApplication_5.CustomLayout">
    <Label Text="{Binding TheContent}"/>
</StackLayout>

请参考自定义布局:

<CollectionView ItemsSource="{Binding DataCollection}" >
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <local:CustomTemplate />
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>