c# - ListView 内的 Xamarin Forms 命令绑定(bind)不起作用

标签 c# listview mvvm xamarin.forms data-binding

我试图将命令绑定(bind)到 ListView 内的按钮,但没有成功。我遵循此处发布的所有其他答案,例如: Xamarin Forms Button Command binding inside a ListView 实际的结果是什么也没有发生。我注意到的一件事是,当我在 x:Reference 之后输入时,Visual Studio 建议我只是 GridWebcam,就像它没有看到其他引用元素一样。我能做什么?

这是我的代码:

    <ContentPage 
             ...
             x:Name="WebcamList">
    <ContentPage.Resources>
        ...
    <ContentPage.Content>
        <ListView ItemsSource="{Binding ListOfWebcam}"
                  SeparatorVisibility="None"
                  CachingStrategy="RecycleElement"
                  RowHeight="250"
                  VerticalOptions="FillAndExpand"
                  x:Name="ListWebcam">

            <ListView.Header>
                <StackLayout x:Name="HeaderStackLayout"
                                   Padding="5,25,0,30"
                                   Orientation="Horizontal"
                                   HorizontalOptions="FillAndExpand">

                        <Label  x:Name="LabelHeader"
                                  Text="Webcam:"
                                  FontSize="Large"
                                  FontAttributes="Bold"
                                  TextColor="{x:Static statics:Palette.PrimaryColor}"
                                  VerticalOptions="Center"
                                  HorizontalOptions="Start" Margin="10,0,0,0"/>
                </StackLayout>
            </ListView.Header>

            <ListView.ItemTemplate>
                <DataTemplate>

                    <controls:ExtendedViewCell IsEnabled="False">
                        <controls:ExtendedViewCell.View>
                            <Grid x:Name="GridWebcam">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <Frame Grid.Column="1"
                                       Grid.RowSpan="2"
                                       CornerRadius="20"
                                       BackgroundColor="{x:Static statics:Palette.PrimaryColor}"
                                       VerticalOptions="FillAndExpand"
                                       HorizontalOptions="FillAndExpand"
                                       HasShadow="True"
                                       Margin="5,10">

                                    <StackLayout>

                                        <Label Text="{Binding t_str_vid,Converter={StaticResource WebcamNameConverter}}"
                                               FontSize="Medium"
                                               TextColor="White"
                                               FontAttributes="Bold"
                                               HorizontalOptions="FillAndExpand"
                                               VerticalOptions="FillAndExpand">

                                        </Label>

                                        <Label TextColor="White"
                                               FontSize="Medium"
                                               Text="{Binding direzione,Converter={StaticResource DirectionToStringConverter}}"/>

                                        <StackLayout Orientation="Horizontal">
                                            <ffimageloading:CachedImage LoadingPlaceholder="Rolling.gif"
                                                                        DownsampleToViewSize="False"
                                                                        VerticalOptions="FillAndExpand"
                                                                        HorizontalOptions="StartAndExpand"
                                                                        Source="{Binding image1}"/>

                                            <iconize:IconButton Text="fas-play-circle"
                                                                FontSize="50"
                                                                HorizontalOptions="EndAndExpand"
                                                                VerticalOptions="EndAndExpand"
                                                                TextColor="White"
                                                                Command="{Binding BindingContext.OpenVideoWebcamCommand, Source={x:Reference WebcamList}}"
                                                                CommandParameter="{Binding .}"
                                                                BackgroundColor="Transparent"/>
                                        </StackLayout>
                                    </StackLayout>
                                </Frame>
                            </Grid>
                        </controls:ExtendedViewCell.View>
                    </controls:ExtendedViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>
      ```
public class WebcamListViewModel : BaseViewModel
{
    public ICommand OpenVideoWebcamCommand { set; get; }

    private List<Webcam> _ListOfWebcam { get; set; }
    public List<Webcam> ListOfWebcam
    {
        get { return _ListOfWebcam; }
        set
        {
            _ListOfWebcam = value;
            OnPropertyChanged();
        }
    }

    private Task DownloadFramesTask;

    CancellationTokenSource tokenSourceDownloadFrames = new CancellationTokenSource();

    CancellationToken cancellationTokenDownloadFrames;

    public WebcamListViewModel(INavigationService navigationService, IApiAutostradeManagerFactory apiAutostradeManagerFactory) : base(navigationService,apiAutostradeManagerFactory)
    {

        OpenVideoWebcamCommand = new Command<Webcam>(async (webcam) => {
            await navigationService.NavigateAsync(Locator.WebcamVideoPopUpPage);
            Messenger.Default.Send(new InfoWebcamVideoMessage(webcam.c_mpr, webcam.c_uuid, webcam.t_str_vid));
        });     
}

最佳答案

嗯,这可能与这个神秘的controls:ExtendedViewCell有关。你的:)

您是否还禁用了 ListView 选择:<ListView ... SelectionMode="None" />

关于c# - ListView 内的 Xamarin Forms 命令绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360670/

相关文章:

c# - 使用 Ninject 跨类传递参数

c# - Ninject 基于参数类型的条件绑定(bind)

android - 来自枚举的 AlertDialog Builder setSingleChoiceItems

c# - 从 DataGrid MVVM 中的 TextBox 读取文本(wpf 数据绑定(bind))

c# - 将间接依赖项添加到输出

c# - XElement 和列表<T>

android - 任何关于如何在向下滚动时将项目添加到 ListView 的教程,同时显示进度项目?

javascript - react-native limit 列表项

android - 在android中实现MVVM时对网络层的错误处理感到困惑,如何通知用户有问题?

c# - WPF Multibinding - 需要使用 Relay 命令