Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ListView自定义ViewCell中带有Tap GestureRecognitor的标签不工作_Listview_Xamarin - Fatal编程技术网

ListView自定义ViewCell中带有Tap GestureRecognitor的标签不工作

ListView自定义ViewCell中带有Tap GestureRecognitor的标签不工作,listview,xamarin,Listview,Xamarin,我有一个带有自定义ViewCell的listview,它有一个带有TapGestureRecognitor的标签,我想在父绑定上下文中激发该标签。此视图单元绑定到“某物”视图模型,某物视图模型是某物更大视图模型的一部分 public class CustomViewCell : ViewCell { #region bindable properties public static readonly BindableProperty ToggleCommandPropert

我有一个带有自定义
ViewCell
的listview,它有一个带有
TapGestureRecognitor
标签,我想在父绑定上下文中激发该标签。此视图单元绑定到“某物”视图模型,某物视图模型是某物更大视图模型的一部分

public class CustomViewCell : ViewCell 
{

    #region bindable properties

    public static readonly BindableProperty ToggleCommandProperty =
        BindableProperty.Create(nameof(ToggleCommand), typeof(Command), typeof(CustomViewCell), null);

    public ICommand ToggleCommand
    {
        get { return (ICommand)GetValue(ToggleCommandProperty); }
        set { SetValue(ToggleProperty, value); }
    }

    #endregion


    public CustomViewCell
    {
        Grid grid = new Grid
        {
            ColumnSpacing = 3,
            RowSpacing = 0
        };

        //..........
        // adding other elements here
        //..........

        var addLabel = new Label
        {
            Text = "+ Add",
            FontSize = 11,
            WidthRequest = 40,
            Margin = new Thickness(0, 0, 0, 5),
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalTextAlignment = TextAlignment.Center
        };

        addLabel.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = ToggleCommand,
            CommandParameter = BindingContext
        });

        grid.Children.Add(addLabel, 7, 1);
        Grid.SetRowSpan(addLabel, 2);

        View = grid;
    }
}
SomethingBiggerViewModel
,如果点击标签,则应执行命令:

public class SomethingBiggerViewModel 
{
     public ICommand ToggleCommand { get; set; }

     public SomethingBiggerViewModel() 
     {
          ToggleCommand = new Command<SomethingViewModel>(async (something) => await ExecuteToggleCommand(something));
     }

    async Task ExecuteToggleCommand(SomethingViewModel something)
    {
        await _pageService.DisplayAlert("title", "item clicked " + something.Name, "Cancel");
    }
}
公共类SomethingBiggerViewModel
{
公共ICommand ToggleCommand{get;set;}
公共SomethingBiggerViewModel()
{
ToggleCommand=new命令(异步(某物)=>await-ExecuteToggleCommand(某物));
}
异步任务ExecuteToggleCommand(SomethingViewModel something)
{
wait_pageService.DisplayAlert(“标题”,“点击项目”+某物.Name,“取消”);
}
}
在页面中:

<ListView Grid.Row="3" 
            VerticalOptions="FillAndExpand"
            CachingStrategy="RecycleElement"
            x:Name="linesListView" 
            RowHeight="{Binding LineRowHeight}"
            ItemsSource="{Binding Lines}"
            SeparatorColor="#bfbfbf">
    <ListView.ItemTemplate>
        <DataTemplate>
            <views:CustomViewCell ToggleCommand="{Binding Source={x:Reference ThePage}, Path=BindingContext.ToggleCommand}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

上面的代码可以编译,但当我运行它时,它会给我一个错误“但它仍在工作”,即“SomethingViewModel”没有“ToggleCommand”


如何执行此操作?

能否显示完整的viewmodel和axml
x:在此处引用页面
是否要绑定到页面的视图模型?@LeoZhu MSFT我没有绑定到父视图模型(
somethingbiger
)。
ToggleCommand
SomethingBiggerViewModel
中定义,您应该绑定
SomethingBiggerViewModel