【问题标题】:Xamarin Forms Command Binding inside ListView is not workingListView 中的 Xamarin 表单命令绑定不起作用
【发布时间】:2019-12-13 02:23:45
【问题描述】:

我正在尝试将命令绑定到 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));
        });     
}

【问题讨论】:

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


【解决方案1】:

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

您是否也禁用了 ListView 选择:&lt;ListView ... SelectionMode="None" /&gt;

【讨论】:

  • 感谢 Roubachof,我一直在寻找没有问题的问题。问题只是选择了禁用的 Listview 项目。我在 Visual Studio 中过于信任它并没有建议使用 ListView bindingContext。所以,我从 ExtendedViewCell 标记中删除了这一行:IsEnabled="False",现在它可以工作了。再次感谢。
  • 我也面临同样的问题:stackoverflow.com/questions/67970649/…。这里我需要选择listview item,如果我们不选择图片的话。
【解决方案2】:

正如 Roubachof 所说,我不知道是否与controls:ExtendedViewCell 有关,请检查您是否有绑定 BindingContext,然后您可以看一下以下代码:

 <StackLayout>
        <ListView x:Name="listview1" ItemsSource="{Binding persons}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Id}" />
                            <Label Text="{Binding name}" />
                            <Button
                                Command="{Binding BindingContext.command, Source={x:Reference listview1}}"
                                CommandParameter="{Binding Id}"
                                Text="Delete item" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

    public partial class Page1 : ContentPage
{
    public ObservableCollection<person> persons { get; set; }
    public RelayCommand1 command { get; set; }

    public Page1 ()
    {
        InitializeComponent ();
        persons = new ObservableCollection<person>();

        for(int i =0;i<20;i++)
        {
            person p = new person()
            {
                Id = i,
                name = "cherry" + i

            };
            persons.Add(p);

            command = new RelayCommand1(obj => method1((int)obj));
        }
        this.BindingContext = this;
    }
    public void method1(int Id)
    {
        persons.RemoveAt(Id);
        //IEnumerable<person> list = persons.Where(x => x.Id == Id);

        //foreach (person m in list)
        //{

        //}
    }
}

public class person
{
    public int Id { get; set; }
    public string name { get; set; }
}

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2017-08-18
    • 2021-07-21
    • 2016-10-10
    相关资源
    最近更新 更多